Clicking column headings to sort and reverse sort
Moderator: General Moderators
- cstallins
- Forum Newbie
- Posts: 1
- Joined: Sun Jan 06, 2008 3:23 pm
- Location: Pleasanton, California, USA
Clicking column headings to sort and reverse sort
Using radio buttons and a Submit button I have successfully coded the PHP/MySQL that sorts my table of db records - I capture the radio button value in $_POST['sortOrder'] and pass that value to my query. It works very well, but is a little less than slick - the visitor has to select both a radio button and the Submit button.
Some web sites offer the ability to sort a table of records by clicking on a column heading, then reverse the sort order by again clicking on the column heading. I have two column headings for which I would like to enable this functionality. Can you share with me how one passes a value from a piece of text (a column heading wrapped in an anchor tag?) to my PHP/MySQL query?
Thank you very much for your time.
Curtis
Some web sites offer the ability to sort a table of records by clicking on a column heading, then reverse the sort order by again clicking on the column heading. I have two column headings for which I would like to enable this functionality. Can you share with me how one passes a value from a piece of text (a column heading wrapped in an anchor tag?) to my PHP/MySQL query?
Thank you very much for your time.
Curtis
If your header is a link, you could have a query string in the link like:
If the value exists, modify your sql to use that cols ordering
and then in the php you can see if orderby exists with:
Code: Select all
if(isset($_GET['orderby'])){
//do something with the $_GET['orderby'] value
}- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Guy above beat me to it.
Last edited by markusn00b on Mon Jan 07, 2008 10:29 am, edited 1 time in total.
If your table cells have just text, you could do client-side sorting as well. It's not applicable in all cases, but certainly is more responsive & less server intensive than server-side.
I've used: http://www.kryogenix.org/code/browser/sorttable/ successfully, and hear http://tablesorter.com/docs/ (which uses JQuery is pretty good too)
I've used: http://www.kryogenix.org/code/browser/sorttable/ successfully, and hear http://tablesorter.com/docs/ (which uses JQuery is pretty good too)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Tablesorter is a great tool. But I would always recommend a server side fallback for those that do not have JS enabled.
What you want to do is pretty common. Just remember that the first run generally comes from POST but everything else after that comes from GET. You will not only need to pass sort orders by the query string but you will also have to pass the search term and, if you are using paging, the page information as well.
What you want to do is pretty common. Just remember that the first run generally comes from POST but everything else after that comes from GET. You will not only need to pass sort orders by the query string but you will also have to pass the search term and, if you are using paging, the page information as well.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
I agree that sorting must be done server-side when there is a pagination. I love the Shift-click feature of tablesorter which allows user to sort the table by using higher order sorting (i.e. sort by name asc, age desc, sex asc). So, if you implement sorting by using AJAX (that is JS enabled browsers) it would be very nice if you implement this feature 
There are 10 types of people in this world, those who understand binary and those who don't
- cyberblades
- Forum Newbie
- Posts: 1
- Joined: Tue Jan 08, 2008 3:02 am
- Contact:
header sort
that's a good idea to sort with header. can you tell me the example?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA