Page 1 of 1

Clicking column headings to sort and reverse sort

Posted: Sun Jan 06, 2008 3:26 pm
by cstallins
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

Posted: Sun Jan 06, 2008 5:19 pm
by Inkyskin
If your header is a link, you could have a query string in the link like:
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
}
If the value exists, modify your sql to use that cols ordering

Posted: Sun Jan 06, 2008 5:19 pm
by markusn00b
Guy above beat me to it.

Posted: Sun Jan 06, 2008 6:37 pm
by pickle
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)

Posted: Sun Jan 06, 2008 6:43 pm
by Kieran Huggins
tablesorter FTW!

Posted: Sun Jan 06, 2008 9:30 pm
by RobertGonzalez
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.

Posted: Mon Jan 07, 2008 1:05 am
by Mordred
Bah, client-side table sorting *gack* :dubious:
And what do you do when you have 10000 records?
Ajaxify the table if you must, but never ever transport all records just so they can be sorted client-side. Plus, as Everah already pointed out you need the server-side solution anyways.

Posted: Mon Jan 07, 2008 2:34 am
by Kieran Huggins
Everah++

if sorting is necessary you should definitely have the server-side code working BEFORE you replace it with JS - progressive enhancement FTW.

Mordred makes a fine point as well - it really depends whether you use pagination or not.

Posted: Mon Jan 07, 2008 4:37 am
by Inkyskin
Whenever I use Ajax for sorting a table, I don't do it that way tablesorter etc do it - I do an ajax call and replace the entire HTML in the table, including the pagination code. Still slick and quick (If your db is optimized!)

Posted: Mon Jan 07, 2008 6:02 am
by VladSun
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 :)

header sort

Posted: Tue Jan 08, 2008 3:34 am
by cyberblades
that's a good idea to sort with header. can you tell me the example?

Posted: Tue Jan 08, 2008 10:12 am
by RobertGonzalez
Have you tried coding anything yet? Getting someone to give you code around here is a little tough. We rather enjoy helping you, but we rather detest doing things for you.

Posted: Tue Jan 08, 2008 10:40 am
by alex.barylski
What I would like to know is how you get those single line borders around the table cells...

table {border: 1px solid #aeaeae}

or

td {border: 1px solid #aeaeae}

The latter of course causes double borders in many places...any suggestions? :)

Posted: Tue Jan 08, 2008 11:34 am
by RobertGonzalez
Look at the standard attributes for the TABLE tag. Specifically "frames" and "rules".

Posted: Tue Jan 08, 2008 11:55 am
by feyd
Useful Posts has a thread linked about this subject matter.