Clicking column headings to sort and reverse sort

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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

Post 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
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post 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
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

Guy above beat me to it.
Last edited by markusn00b on Mon Jan 07, 2008 10:29 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

tablesorter FTW!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post 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!)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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 :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
cyberblades
Forum Newbie
Posts: 1
Joined: Tue Jan 08, 2008 3:02 am
Contact:

header sort

Post by cyberblades »

that's a good idea to sort with header. can you tell me the example?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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? :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Look at the standard attributes for the TABLE tag. Specifically "frames" and "rules".
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts has a thread linked about this subject matter.
Post Reply