Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry,

 

Just want to know if it is possible to make the top column links sort data ascending as well as descending with php as discussed in chapter 10 - and what code do I need to change?

 

Thank you.

 

Apache 2.2.21

PHP 5.3.8

MySQL 5.0.8

Link to comment
Share on other sites

I seem to recall Larry talking about this in the 3rd edition of this book, but it seems like he doesn't in this book.

Anyway, you can pretty easily alternate between ascending and descending order by expanding the switch statement on page 324 and then taking that into account for step 5 on page 326.

For example, if the user clicks on the First Name column, then when you write the HTML for the table header links, instead of writing "sort=fn" for the First Name column, you should write something like "sort=rfn".

Does that make sense?

  • Upvote 1
Link to comment
Share on other sites

Thank you for your reply Hartley. I'm still new at coding so it's more 'cut-and-past' for me than anything else. I tried to change the switch statement and HTML as you suggested without any success. I also had a look at the book's 3rd edition but couldn't find anything to help me. Can you be more specific about the expansion of the switch statement and changes to the HTML code?

Link to comment
Share on other sites

I can't be more specific because after buying the 4th edition, I gave my 3rd edition away to a friend. Besides, I was purely going off memory, but the 3rd edition is actually the one I went through myself and I seemed to recall being able to reverse the order of the columns with the code in that book.

 

Anyway, when I have some time later, I'll provide some more details about how to modify the code in the 4th edition to get what you want.

Please be patient. Thank you.

Link to comment
Share on other sites

Okay, I can respond now.

 

The key is to add additional cases to the switch statement on page 324 to account for reverse sorting. Also, you need to keep track of the currently used sort so that if, for example, the First Name column is sorted twice in a row, the second time, you can sort it in descending order, not ascending order. Furthermore, you also need to consider what should happen if the same column is sorted three or more times in a row, which should handle itself automatically, if you design your code the right way.

 

Anyway, let's get down to the code. The easy part is adding the additional cases to the switch statement for the reverse order of the three columns. Please modify the switch statement in the book on page 324 as follows:

 

switch ($sort) {

 case 'ln':

   $order_by = 'last_name ASC';

   break;

 case 'lnr':

   $order_by = 'last_name DESC';

   break;

 case 'fn':

   $order_by = 'first_name ASC';

   break;

 case 'fnr':

   $order_by = 'first_name DESC';

   break;

 case 'rd':

   $order_by = 'registration_date ASC';

   break;

 case 'rdr':

   $order_by = 'registration_date DESC';

   break;

 default:

   $order_by = 'registration_date ASC';

   $sort = 'rd';

}

 

Note that I just appended the letter r (for "reverse") onto the end of the existing sort codes for the reverse sorts. Each of the reverse cases is almost the same as the original cases; all we have to do is go in descending order instead of ascending order.

 

In order to keep track of the currently sorted column and to make it reversible, there are a variety of approaches, but my personal preference is to use variables for the three possible column sorts, and then append the letter r onto the one that can be reversed.

 

To do this, add the following three variable declarations above the switch statement and then modify the switch statement as follows:

 

$ln_sort = 'ln';

$fn_sort = 'fn';

$rd_sort = 'rd';

switch ($sort) {

 case 'ln':

   $order_by = 'last_name ASC';

   $ln_sort = 'lnr';

   break;

 case 'lnr':

   $order_by = 'last_name DESC';

   break;

 case 'fn':

   $order_by = 'first_name ASC';

   $fn_sort = 'fnr';

   break;

 case 'fnr':

   $order_by = 'first_name DESC';

   break;

 case 'rd':

   $order_by = 'registration_date ASC';

   $rd_sort = 'rdr';

   break;

 case 'rdr':

   $order_by = 'registration_date DESC';

   break;

 default:

   $order_by = 'registration_date ASC';

   $rd_sort = 'rdr';

   $sort = 'rd';

}

 

Essentially, for all the cases where the order is ascending, we need to change the corresponding sort so that it will be reversed the next time.

 

As a final step, the sort links in the top row of the table need to be changed as follows to use the variables instead of static values:

 

<td align="left"><b><a href="view_users.php?sort=' . $ln_sort . '>Last Name</a></b></td>
<td align="left"><b><a href="view_users.php?sort=' . $fn_sort . '>First Name</a></b></td>
<td align="left"><b><a href="view_users.php?sort=' . $rd_sort . '>Date Registered</a></b></td>

 

By doing that, your columns should now toggle between ascending and descending sorts.

Please note that I didn't test out my code, so I'm not sure if it works.

Please let me know.

Thanks.

  • Upvote 1
Link to comment
Share on other sites

Hi HartleySan. Thank you so much for your help. I applied your code and the sorts work perfectly - I just had to add the closing double quotes after the variables to the sort links of the top table row (last snippet of code), which you left out by accident:)

 

Just two related questions:

  1. Larry ends the related switch conditional in his book with a 'break' after the default and you left it out, although your code also works perfectly fine. Is the last break necessary?
  2. Is the " '$rd_sort = 'rdr' " part of the default code necessary as the "registration date" sorts seem to work perfectly fine without it?

Thanks again for your assistance.

Link to comment
Share on other sites

 Share

×
×
  • Create New...