Page 1 of 1
table header link
Posted: Wed Mar 04, 2009 2:07 am
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
Re: table header link
Posted: Wed Mar 04, 2009 2:38 am
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>