Well, I do as dapuxter. I use JS. It is nice not to have to hit your db everytime the user wants to sort a table of results. Where JS will not work is when you paginate the data (unless you do cool stuff with AJAX or JSONRPC). Another time you do NOT want to use JS is if you have large tables. JS is dependent on client speed. So if a user has a slow machine, the JS will seem to "lock-up" their computer while it sorts. If you want to go the JS route here you go:
http://kryogenix.org/code/browser/sorttable/
It is free to use under the MIT license.
Now, let's say you want to paginate or, you want to ensure the sorting will work if your user turns off JS (yes some users actually turn it off). Here is what you can do:
<?
$strOrder = $_GET["sort"] == 'desc' ? 'asc' : 'desc';
echo '<a href="' . $_SERVER['PHP_SELF'] . '?order=id&sort=' . $strOrder . '">ID</a>';
?>
I want to point out a few things:
1) don't use $PHP_SELF it requires registered globals be turned on, this is a bad thing, security hole, use $_SERVER['PHP_SELF'] instead. If you move your code to a web hosting server, more than likely this will be turned off and your script will break.
2) The first statement, it is called a ternary, some people have not seen that before. If you have not it is the same as:
if( $_GET['sort'] == 'desc' )
{
$strOrder = 'asc';
}
else
{
$strOrder = 'desc';
}
you might agree, cleaner to do a ternary.
3) I also removed the idct get var, it is unnecessary, you already have a "flag" in the value of "sort".
4) Now this code is just going to switch the value of sort in the header, you would have to obviously add the ORDER BY part to your SQL Query. I am assuming you knew this and that is why you did not post that code. If you do not, post back and I will show you how.
5) NOW, this is very important. Depending on your version of PHP $GET and $SERVER may not be available. They are called superglobals (transends all levels of variable scope). If you are using PHP prior to 4.1.2 those constructs do not exist. You will need to do the long version: $HTTP_GET_VARS and $HTTP_SERVER_VARS (I think, it has been a long time).
Hope this helps,
Mike