The Article By Patrick Nichols
When displaying data tables, how do you usually sort the information? Do you sort by the field that you think is the most important to the visitor? For example, it's perfectly acceptable to list entries chronologically; where the newest is shown first. But what if the user wants the data organized differently? We could let them choose.
Background
Let's say we have a MySQL table containing the conference registrations and a solution for conference organizers to access the information. To make it easier for processing the registrations, the entries are listed by the submission date in reverse-chronological order. That way the newest orders appear on top.
The conference organizers, however, might need to know if a specific customer is registered. Having the information sorted by date increases the difficulty of finding the customer. That's where letting the user change the sort order can help.
Note: this post won't show the entire script and assumes you're familiar with HTML, PHP, and MySQL. If you have question, please post them in the comments below.
Existing Code
First, the registrations are pulled from the database, processed, and stored in the $registrantList variable to be displayed later.
1 <?php 2 //GET THE LIST OF REGISTRANTS 3 $registrantList = ''; 4 $sql = "SELECT first_name, last_name, date FROM conference_registrants ORDER BY date DESC"; 5 $result = mysql_query($sql); 6 while($row = mysql_fetch_array($result)) { 7 $registrantList .= '<tr><td>' . htmlentities($row['first_name']) . '</td><td>' . htmlentities($row['last_name']) . '</td><td>' . $row['date'] . '</td></tr>'; 8 } 9 ?>
Note that the information is sorted by the "date" field in descending order. We'll figure out how to change this on the fly later. For now, the data is displayed by date.
1 <?php 2 //DISPLAY THE CONFERENCE REGISTRANTS 3 print '<table cellpadding="3" cellspacing="1" border="1">'; 4 print '<tr>'; 5 print '<th scope="col">First Name</th>'; 6 print '<th scope="col">Last Name</th>'; 7 print '<th scope="col">Date Registered</th>'; 8 print '</tr>'; 9 print $registrantList; 10 print '</table>'; 11 ?>
The existing code is fairly straight forward, so hopefully everything makes sense. Let's move ahead to dynamically changing the sort order.
Changing Sort Order Dynamically
As mentioned earlier, conference organizers should be able to sort the registrants as needed. To do that, we'll pass a flag through the program that indicates the column to sort by. So, let's add some links to the HTML table that displays the registrants. The "First Name" heading will look like:
1 <?php 2 print '<th scope="col">'; 3 print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; 4 print '</th>'; 5 ?>
The link points to the same page we're currently modifying and passes a GET variable called "orderBy" when clicked. Links are also needed for the other headings.
1 <?php 2 //DISPLAY THE CONFERENCE REGISTRANTS 3 print '<table cellpadding="3" cellspacing="1" border="1">'; 4 print '<tr>'; 5 print '<th scope="col">'; 6 print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; 7 print '</th>'; 8 print '<th scope="col">'; 9 print '<a href="/registrants.php?orderBy=last_name">Last Name</a>'; 10 print '</th>'; 11 print '<th scope="col">'; 12 print '<a href="/registrants.php?orderBy=date_desc">Date Registered</a>'; 13 print '</th>'; 14 print '</tr>'; 15 print $registrantList; 16 print '</table>'; 17 ?>
With the flag being passed, the script needs to be modified to process it. But we should probably make sure the flag exists first. If it doesn't, the value would be defaulted to the date in descending order.
1 <?php 2 //IF THE FLAG HASN'T BEEN SET YET, SET THE DEFAULT 3 if(!isset($_GET['orderBy'])) { 4 $_GET['orderBy'] = 'date_desc'; 5 } 6 ?>
Since the GET variable could have been tampered with, we need to make sure the value is valid. Otherwise, the program may not function as expected or worse—damage the database.
1 <?php 2 //FIGURE OUT HOW TO SORT THE TABLE 3 switch($_GET['orderBy']) { 4 case 'first_name': 5 case 'last_name': 6 $sql_orderBy = $_GET['orderBy']; 7 break; 8 9 default: 10 $_GET['orderBy'] = 'date_desc'; 11 $sql_orderBy = 'date DESC'; 12 } 13 ?>
In addition to the GET variable being sanitized, we now have a variable ($sql_orderBy) which will be used for the order by clause in the following SQL query:
1 <?php 2 //GET THE LIST OF REGISTRANTS 3 $registrantList = ''; 4 $sql = "SELECT first_name, last_name, date FROM 2011_registrants ORDER BY $sql_orderBy"; 5 $result = mysql_query($sql); 6 while($row = mysql_fetch_array($result)) { 7 $registrantList .= '<tr><td>' . htmlentities($row['first_name']) . '</td><td>' . htmlentities($row['last_name']) . '</td><td>' . $row['date'] . '</td></tr>'; 8 } 9 ?>
With all the code in place, visitors can now sort the data by clicking the column headings. But there's still one more thing to do. When sorting by first name, for example, the "First Name" heading doesn't need to be clickable any more. That can be fixed by hiding the link if the orderBy flag is set to "first_name".
1 <?php 2 print '<th scope="col">'; 3 if($_GET['orderBy'] == 'first_name') { print 'First Name'; } 4 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 5 print '</th>'; 6 ?>
As an added bonus, hiding the links provides a hint to which column the data is sorted by. We just need to do the same with the other columns.
1 <?php 2 //DISPLAY THE CONFERENCE REGISTRANTS 3 print '<table cellpadding="3" cellspacing="1" border="1">'; 4 print '<tr>'; 5 print '<th scope="col">'; 6 if($_GET['orderBy'] == 'first_name') { print 'First Name'; } 7 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 8 print '</th>'; 9 print '<th scope="col">'; 10 if($_GET['orderBy'] == 'last_name') { print 'Last Name'; } 11 else { print '<a href="/registrants.php?orderBy=last_name">Last Name</a>'; } 12 print '</th>'; 13 print '<th scope="col">'; 14 if($_GET['orderBy'] == 'date_desc') { print 'Date Registered'; } 15 else { print '<a href="/registrants.php?orderBy=date_desc">Date Registered</a>'; } 16 print '</th>'; 17 print '</tr>'; 18 print $registrantList; 19 print '</table>'; 20 ?>
Final Code
In the end, here is what the completed code looks like all together:
1 <?php 2 //IF THE FLAG HASN'T BEEN SET YET, SET THE DEFAULT 3 if(!isset($_GET['orderBy'])) { 4 $_GET['orderBy'] = 'date_desc'; 5 } 6 7 //FIGURE OUT HOW TO SORT THE TABLE 8 switch($_GET['orderBy']) { 9 case 'first_name': 10 case 'last_name': 11 $sql_orderBy = $_GET['orderBy']; 12 break; 13 14 default: 15 $_GET['orderBy'] = 'date_desc'; 16 $sql_orderBy = 'date DESC'; 17 } 18 19 //GET THE LIST OF REGISTRANTS 20 $registrantList = ''; 21 $sql = "SELECT first_name, last_name, date FROM 2011_registrants ORDER BY $sql_orderBy"; 22 $result = mysql_query($sql); 23 while($row = mysql_fetch_array($result)) { 24 $registrantList .= '<tr><td>' . htmlentities($row['first_name']) . '</td><td>' . htmlentities($row['last_name']) . '</td><td>' . $row['date'] . '</td></tr>'; 25 } 26 27 //DISPLAY THE CONFERENCE REGISTRANTS 28 print '<table cellpadding="3" cellspacing="1" border="1">'; 29 print '<tr>'; 30 print '<th scope="col">'; 31 if($_GET['orderBy'] == 'first_name') { print 'First Name'; } 32 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 33 print '</th>'; 34 print '<th scope="col">'; 35 if($_GET['orderBy'] == 'last_name') { print 'Last Name'; } 36 else { print '<a href="/registrants.php?orderBy=last_name">Last Name</a>'; } 37 print '</th>'; 38 print '<th scope="col">'; 39 if($_GET['orderBy'] == 'date_desc') { print 'Date Registered'; } 40 else { print '<a href="/registrants.php?orderBy=date_desc">Date Registered</a>'; } 41 print '</th>'; 42 print '</tr>'; 43 print $registrantList; 44 print '</table>'; 45 ?>
Related Posts
Ps.The Origin Source:http://www.cyberscorpion.com/2011-11/sorting-html-data-tables-using-the-header-row-with-php/