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.
Sortable Table headings
Moderator: General Moderators
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 againBurrito 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.
ex:
then your php:
that doesn't take into account ascending or descending, but you can figure that one....
Code: Select all
<td><a href="samepage.php?sortby=name">Name</a></td> <td><a href="samepage.php?sortby=address">Address</a></td>Code: Select all
$query = "SELECT * FROM `someTable` ORDER BY {$_GET['sortby']}";