Sortable Table headings

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
zen2200
Forum Newbie
Posts: 2
Joined: Tue Oct 03, 2006 10:09 am

Sortable Table headings

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
zen2200
Forum Newbie
Posts: 2
Joined: Tue Oct 03, 2006 10:09 am

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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....
Post Reply