Sorting HTML Data Tables Using the Header Row with PHP(2)
The Article By Patrick Nichols
Sorting HTML Data Tables Part 2: Dynamically Sort in Ascending and Descending Order
In last week's post we looked at dynamically sorting HTML data tables. But we only talked about sorting the columns in either ascending or descending. If the user is looking for a last name that appears near the end of the alphabet and the column is sorted from A to Z, they may have a lot of names to go through before finding the one they want. Instead we could provide an option for sorting in both descending and ascending order.
So let's take final code from last week:
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 ?>
If you haven't done so already, you may want to review last week's post (Sorting HTML Data Tables Using the Header Row with PHP(1)) which briefly describes what each part of the code does. If you're still unsure about something, feel free to mention it in the comments section below…since there doesn't seem to be any questions, let's get rolling.
Updating the Code
To sort the columns in both ascending and descending order, we'll need a few more values for the order by flag. Let's start by adding them to the first name heading.
1 <?php 2 print '<th scope="col">'; 3 if($_GET['orderBy'] == 'first_name') { print '<a href="/registrants.php?orderBy=first_name_desc">First Name<img src="arrowUp.gif" alt="Ascending Order (A to Z)" /></a>'; } 4 elseif($_GET['orderBy'] == 'first_name_desc') { print '<a href="/registrants.php?orderBy=first_name">First Name<img src="arrowDown.gif" alt="Descending Order (Z to A)" /></a>'; } 5 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 6 print '</th>'; 7 ?>
The table heading will now take one of three states:
- If sorting first name in ascending order, display a link to sort in descending order
- Else…if sorting first name in descending order, display a link to sort in ascending order
- Else…display a link to sort by whatever the default order is for that column which will be ascending order for the first name
Note that the first two states contain an image that indicates the direction the column is being sorted. The third doesn't have an image since the data table is sorted by another column. Next we'll modify the other columns so they have the same setup.
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 '<a href="/registrants.php?orderBy=first_name_desc">First Name<img src="arrowUp.gif" alt="Ascending Order (A to Z)" /></a>'; } 7 elseif($_GET['orderBy'] == 'first_name_desc') { print '<a href="/registrants.php?orderBy=first_name">First Name<img src="arrowDown.gif" alt="Descending Order (Z to A)" /></a>'; } 8 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 9 print '</th>'; 10 print '<th scope="col">'; 11 if($_GET['orderBy'] == 'last_name') { print '<a href="/registrants.php?orderBy=last_name_desc">Last Name<img src="arrowUp.gif" alt="Ascending Order (A to Z)" /></a>'; } 12 elseif($_GET['orderBy'] == 'last_name_desc') { print '<a href="/registrants.php?orderBy=last_name">Last Name<img src="arrowDown.gif" alt="Descending Order (Z to A)" /></a>'; } 13 else { print '<a href="/registrants.php?orderBy=last_name">Last Name</a>'; } 14 print '</th>'; 15 print '<th scope="col">'; 16 if($_GET['orderBy'] == 'date') { print '<a href="/registrants.php?orderBy=date_desc">Date Registered<img src="arrowUp.gif" alt="Ascending Order (Oldest to Newest)" /></a>'; } 17 elseif($_GET['orderBy'] == 'date_desc') { print '<a href="/registrants.php?orderBy=date">Date Registered<img src="arrowDown.gif" alt="Descending Order (Newest to Oldest)" /></a>'; } 18 else { print '<a href="/registrants.php?orderBy=date_desc">Date Registered</a>'; } 19 print '</th>'; 20 print '</tr>'; 21 print $registrantList; 22 print '</table>'; 23 ?>
We'll also need to modify the code for processing the order by flag.
1 <?php 2 //FIGURE OUT HOW TO SORT THE TABLE 3 switch($_GET['orderBy']) { 4 case 'date': 5 case 'first_name': 6 case 'last_name': 7 $sql_orderBy = $_GET['orderBy']; 8 break; 9 10 case 'date_desc': 11 case 'first_name_desc': 12 case 'last_name_desc': 13 $sql_orderBy = substr($_GET['orderBy'], 0, -5) . ' DESC'; 14 break; 15 16 default: 17 $_GET['orderBy'] = 'date_desc'; 18 $sql_orderBy = 'date DESC'; 19 } 20 ?>
Now when the flag is set to "date", "first_name", or "last_name"; the SQL query sorts the data by the selected column. But if the flag is set to "date_desc", "first_name_desc", or "last_name_desc"; it sorts by the column in descending order. Note that the substr() function is there to get the column name by chopping off the last five characters ("_desc"). The rest of the code is the same as last week.
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 'date': 10 case 'first_name': 11 case 'last_name': 12 $sql_orderBy = $_GET['orderBy']; 13 break; 14 15 case 'date_desc': 16 case 'first_name_desc': 17 case 'last_name_desc': 18 $sql_orderBy = substr($_GET['orderBy'], 0, -5) . ' DESC'; 19 break; 20 21 default: 22 $_GET['orderBy'] = 'date_desc'; 23 $sql_orderBy = 'date DESC'; 24 } 25 26 //GET THE LIST OF REGISTRANTS 27 $registrantList = ''; 28 $sql = "SELECT first_name, last_name, date FROM 2011_registrants ORDER BY $sql_orderBy"; 29 $result = mysql_query($sql); 30 while($row = mysql_fetch_array($result)) { 31 $registrantList .= '<tr><td>' . htmlentities($row['first_name']) . '</td><td>' . htmlentities($row['last_name']) . '</td><td>' . $row['date'] . '</td></tr>'; 32 } 33 34 //DISPLAY THE CONFERENCE REGISTRANTS 35 print '<table cellpadding="3" cellspacing="1" border="1">'; 36 print '<tr>'; 37 print '<th scope="col">'; 38 if($_GET['orderBy'] == 'first_name') { print '<a href="/registrants.php?orderBy=first_name_desc">First Name<img src="arrowUp.gif" alt="Ascending Order (A to Z)" /></a>'; } 39 elseif($_GET['orderBy'] == 'first_name_desc') { print '<a href="/registrants.php?orderBy=first_name">First Name<img src="arrowDown.gif" alt="Descending Order (Z to A)" /></a>'; } 40 else { print '<a href="/registrants.php?orderBy=first_name">First Name</a>'; } 41 print '</th>'; 42 print '<th scope="col">'; 43 if($_GET['orderBy'] == 'last_name') { print '<a href="/registrants.php?orderBy=last_name_desc">Last Name<img src="arrowUp.gif" alt="Ascending Order (A to Z)" /></a>'; } 44 elseif($_GET['orderBy'] == 'last_name_desc') { print '<a href="/registrants.php?orderBy=last_name">Last Name<img src="arrowDown.gif" alt="Descending Order (Z to A)" /></a>'; } 45 else { print '<a href="/registrants.php?orderBy=last_name">Last Name</a>'; } 46 print '</th>'; 47 print '<th scope="col">'; 48 if($_GET['orderBy'] == 'date') { print '<a href="/registrants.php?orderBy=date_desc">Date Registered<img src="arrowUp.gif" alt="Ascending Order (Oldest to Newest)" /></a>'; } 49 elseif($_GET['orderBy'] == 'date_desc') { print '<a href="/registrants.php?orderBy=date">Date Registered<img src="arrowDown.gif" alt="Descending Order (Newest to Oldest)" /></a>'; } 50 else { print '<a href="/registrants.php?orderBy=date_desc">Date Registered</a>'; } 51 print '</th>'; 52 print '</tr>'; 53 print $registrantList; 54 print '</table>'; 55 ?>
With a few style sheet (CSS) declarations and a couple images, here is what our table might look like:
Figure 1. First Name Sorted in Ascending Order
Figure 2. Last Name Sorted in Descending Order
Related Posts
Ps.The Origin Source:http://www.cyberscorpion.com/2011-11/sorting-html-data-tables-part-2-dynamically-sort-in-ascending-and-descending-order/