I have a database where, on the admin side, the admin enters football players and their stats for a fantasy football site.
On the front end, users see a tabbed interface (a tab for each position with the relevant players and stats under that tab).
Currently I set the default ORDER BY when I display the results, like this:
$result = mysql_query("SELECT Player, Team, Pass_Yds, Pass_TDs, Int_Thrown, Rush_Yds, Rush_TDs, Overall_Pts, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'QB' ORDER BY Pass_Yds DESC;");
I want to add a Sort By drop down so that the viewer can change how the data is sorted (e.g. if they wanted to sort the quarterback results in order of the most passing touchdowns (Pass_TDs).
How would I go about doing this?
Rather than allow the user to POST a form to change the sort on the server, I recommend allowing the user to simply re-sort on the client. After all, the user already has all of the data delivered to them. There’s no need to post back to the server again and again for the same data just to sort it.
Take a look at jqGrid, for example. You can load your data into that and let it handle the sorting for you (as well as paging, filtering and search, etc. if you want). Tablesorter is another one. And there are many others. (A Google search for something like “jQuery sortable table” will yield a lot of results.)
This would present a better user experience and at the same time reduce the load of the application, since you would only have to deliver the same data to the user once instead of multiple times. Often it’s also a simpler approach because it helps separate the manipulation of the display from the fetching of the data. So your server-side code can stay clean and simple and not have to concern itself with client-side things.