table header link

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
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

table header link

Post by greedyisg00d »

Hi I have 3 fields by default lastname, firstname, e_code which the result is viewed using a table. What I want is a clickable header for example when the user clicks on the header lastname it will sort alphabetically. I already know the query to be used but my problem is the coding part on the table. Any idea? Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: table header link

Post by requinix »

Here's something basic you can look at.

Code: Select all

<?php
 
// sortby is one of the fields to sort by; default is "field1"
if (empty($_GET["sortby"]) || !in_array($_GET["sortby"], array("field1", "field2", "field3")) $_GET["sortby"] = "field1";
// sort is the direction to sort in; default is "asc"
if (empty($_GET["sort"]) || !in_array($_GET["sort"], array("asc", "desc")) $_GET["sort"] = "asc";
 
$query = sprintf("SELECT fields FROM table ORDER BY %s %s", $_GET["sortby"], $_GET["sort"]);

Code: Select all

Field 1 <a href="page.php?sortby=field1&sort=asc">Up</a>|<a href="page.php?sortby=field2&sort=desc">Down</a>
Post Reply