Page 1 of 1

Sortable Table headings

Posted: Tue Oct 03, 2006 10:14 am
by zen2200
Hi guys, im stuck with this problem and im hoping someone can help me out.

i am using php/mysql and I have a table with various fields including the following:

barcode
firstname
lastname
status etc

I query the database (mysql) and it displays the result sorted by barcode. im trying to make it so taht everytime the result is displayed on the html page, the table headers become a link and users can click on them so it gets sorted accordingly. for example if i ran a query that displays all the barcodes in the database, it'd look like this:

barcode firstname lastname status
----------------------------------------------
123 test test available
234 john doe available
343 ted ted not available

etc....

but if i wanted to resort the results dynamically, i can't do that because the headers aren't clickable. so i was wondering if anyone knows a way of making the headers clickable so that when the user clicks on lets say firstname, it sorts the result using firstname field in the database.

thanks in advance.

Posted: Tue Oct 03, 2006 10:38 am
by Burrito
add anchor tags to the headers that return to the same record set. You can either pass the name of header through a url var (easiest way) then use the name of the field in an ORDER BY clause.

Posted: Wed Oct 04, 2006 8:29 am
by zen2200
Burrito wrote:add anchor tags to the headers that return to the same record set. You can either pass the name of header through a url var (easiest way) then use the name of the field in an ORDER BY clause.
thanks for the reply. i tried using $_get to get it working but ended up confusing myself. you suggested using anchor tags to the headers for the headers. can you please tell how how i can go about doing that? i've no clue. thanks again

Posted: Wed Oct 04, 2006 10:05 am
by Burrito
ex:

Code: Select all

<td><a href="samepage.php?sortby=name">Name</a></td> <td><a href="samepage.php?sortby=address">Address</a></td>
then your php:

Code: Select all

$query = "SELECT * FROM `someTable` ORDER BY {$_GET['sortby']}";
that doesn't take into account ascending or descending, but you can figure that one....