Address Book Sort Contacts By Type
- Address Book Sort Contacts By Name
- Address Book Sort Contacts By Type
- Address Book Add New Contact
- Address Book Edit a Contact
- Address Book Delete a Contact
- Address Book Security
This page is a tutorial on an app page that you use to view your address book sorted by the Relationship Type this contact has with you, and you also use it to work from when you are selecting any of the other address book application functions such as delete, add, edit, or sort by name.
As you'll see above from our test page printout, the app page lets you view the name, relationship type, email (it links to your email client so you can email this contact, automatically using the MySQL field Email1 as the email address) and work phone. Feel free to modify the PHP code and column titles so something else comes up rather than these items. Here are the choices:
- id
- First
- Middle
- Last
- Address1
- Address2
- City
- State
- Zip
- Email1
- Email2
- CelPhone
- WorkPhone
- HomePhone
- Fax
- RelationshipType
- RelationshipSpecific
- AssistantAssociate
- ReferredBy
- Website
- Blog
- Notes
- Friend
- Acquaintance
- Professional
- Client
- Group Member
- Business Associate
The two upper corner buttons get you to the sort by name and add new contact functions. The delete and edit functions are specific to the contact person whose row they are in—there are delete and edit function links on every row. There's no limit to the number of entries, except for when your host screams because your 10 million entry table is taking too long to load or too much space on his server. The fields have character limits—usually 20, 40, or 90, except State and Zip get only 10 each and Notes gets 400, since it's a textarea box with liberal allowances. Bad email addresses will be caught and you will be asked to fix them before you can save. (So dumb@dumber is no good, nor is smelly@smellier.smelliest).
Let us look at the script. The CSS is trivial. The include gets you MySQL access if you created it right. The SELECT * FROM addressbook ORDER BY RelationshipType gets everything in the table into the result set and our script puts just the fields First, Middle, Last, RelationshipType, Email and WorkPhone in the address book display, sorted by the RelationshipType field. Note that Name is composed of three fields. If you ever end up with thousands of contacts, feel free to replace * with First, Middle, Last, RelationshipType, Email1, WorkPhone and also use mysql_fetch_assoc() rather than mysql_fetch_array() (the latter gets both numeric and associative indices, while the associative version gets only associative indices) to speed things up, but MySQL and PHP are fast, so we doubt it will make much practical difference for you. If the table is empty, you get sent to Address Book Add New Contact to add contacts.
The }else{ is the start of the code that is run if the table has data in it. Next we use a simple while loop and the PHP mysql_fetch_array() function to get specific data from the table. This function gets a result row as an associative array, a numeric array, or both if you add special parameters to keep it from defaulting to both array indices being fetched. Since it only gets one row in the array, you need the while loop to get them all. With each row's array, you choose which fields you're interested in by use of such code as this:
echo htmlentities(stripslashes($row['First']), ENT_QUOTES). The stripslashes() function unescapes the data that got escaped for security reasons by the mysql_real_escape_string() function as we originally inserted the data into the MySQL database table. The htmlentities() function makes the data safe to display in a browser just in case some wise guy has stuck in some type of hacker code nonsense. The ENT_QUOTES parameter applies to htmlentities() and will convert both double and single quotes to safer display forms (e.g., ' is changed to ').
Note that we don't display emails—we convert them to links with "Email" as link text. First we get the email into $E, using our htmlentities(stripslashes()) double whammy to make it safe, which was already handled in the email filter routine in Address Book Add New Contact which would never have allowed bad stuff in the email address in the first place. Double protection—ya gotta love it. Notice where the single and double quotes go in this line:
echo "<a href='mailto:".$E."?subject=Address Book Mail'>Email</a>"; . Feel free to change the email subject that will go in the email client Subject box while the email address itself ends up in the To box.
Now check out where quotes are in the following:
echo "<a HREF='address-book-edit.php?id=".$row['id']."'>Edit</a>"; . It can be hard to follow. Make sure to understand what is single and what is double, which is hard to see unless you forward-arrow over the line watching where the cursor jumps to.
So then we finish the table and the div, then close up the database. And that's it.
On to the code for the script. Name the following: address-book-sort-by-type.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>PHP Address Book — Sort by Type</TITLE>
<meta name="description" content="PHP Address Book — Sort by Type">
<meta name="keywords" content="PHP Address Book,PHP Address Book Sort by Type,Content Management System,cms,javascript, dhtml, DHTML">
<style type="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;background-color:#cccccc;}
p, li, td {font:13px Verdana; color:black;}
h1 {font:bold 28px Verdana; color:black;text-align:center}
#address {position:absolute; left:50px; top:60px; width:900px; margin:0; padding:10px; background-color:#eee; border:2px solid #000; font:bold 11px Verdana;}
.button1{position:absolute; left:50px; top:26px; width:98px; padding:4px; background-color:#ddd; border:1px solid #000; font:bold 11px Verdana;}
.button2{position:absolute; right:50px; top:26px; width:121px; padding:4px; background-color:#ddd; border:1px solid #000; font:bold 11px Verdana;}
</style>
</head>
<body>
<div class='button1'><a HREF="address-book-sort-by-name.php">Sort by Name</a></div>
<div class='button2'><a HREF="address-book-add.php">Add New Contact</a></div>
<?php
include_once"../config.php";
$res = mysql_query("SELECT * FROM addressbook ORDER BY RelationshipType") or die(mysql_error());
$rows = mysql_num_rows($res);
if($rows==0){echo '<script language="javascript">alert("Address Book empty. Add contacts."); window.location = "address-book-add.php"; </script>';
}else{
echo "<center><h1>PHP Address Book — Sort by Type</h1></center>
<div id='address'><table border='1' width='900'>";
echo "<tr><th>Name</th><th>RelationshipType</th><th>Email</th><th>Work Phone</th><th>Edit</th><th>Delete</th></tr>";
while($row = mysql_fetch_array($res)) {
echo "<tr><td>";
echo htmlentities(stripslashes($row['First']), ENT_QUOTES);
echo " ";
echo htmlentities(stripslashes($row['Middle']), ENT_QUOTES);
echo " ";
echo htmlentities(stripslashes($row['Last']), ENT_QUOTES);
echo "</td><td>";
echo htmlentities(stripslashes($row['RelationshipType']), ENT_QUOTES);
echo "</td><td>";
$E=htmlentities(stripslashes($row['Email1']), ENT_QUOTES);
echo "<a href='mailto:".$E."?subject=Address Book Mail'>Email</a>";
echo "</td><td>";
echo htmlentities(stripslashes($row['WorkPhone']), ENT_QUOTES);
echo "</td><td>";
echo "<a HREF='address-book-edit.php?id=".$row['id']."'>Edit</a>";
echo "</td><td>";
echo "<a HREF='delete-address-book-contact.php?id=".$row['id']."'>Delete</a>";
echo "</td></tr>";
}
echo "</table></div>";
mysql_close();
}
?>
</body>
</html>