I have my code working to sort by a specific column once, but I can't get it to cycle through asc, desc, and default. Right now I can click the header once and it sorts it desc, each click after that doesn't do anything though. I want the first click to sort desc, then the next click to sort asc, then the next click to go back to the default (order by ID asc) and just cycle through those. Here's what I have so far, can anyone help improve my sorting? I don't fully understand how this works, but it seems I'm unable to set $sort to desc. I don't have any idea how to reset it to the default on the third click though.
$field='ID';
$sort='ASC';
$sortOrder='';
$sortISBN='ISBN';
$sortAuthor='Author';
$sortTitle='Title';
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
$sortOrder=' ↓';
}
else
{
$sort='ASC';
$sortOrder=' ↑';
}
}
if(isset($_GET['field']))
{
if($_GET['field']=='ISBN')
{
$field='ISBN';
$sortISBN='ISBN ' . $sortOrder;
}
elseif($_GET['field']=='Author')
{
$field='Author';
$sortAuthor='Author ' . $sortOrder;
}
elseif($_GET['field']=='Title')
{
$field='Title';
$sortTitle='Title ' . $sortOrder;
}
}
Here's the other relevant part of the code:
<?php
$query=mysql_query("select * from Books order by $field $sort") or die(mysql_error());
echo'<table border="1">';
?>
<tr><th colspan="5">Your book list</th>
<td>
<form name="New" action="new.php" method="POST">
<input type="submit" name="New" value="New" title="Add a new entry"/>
</form>
</td>
</tr>
<tr>
<th></th>
<th><a href="index.php?sorting='.$sort.&field=ISBN"><?php echo $sortISBN; ?></th>
<th><a href="index.php?sorting='.$sort.&field=Author"><?php echo $sortAuthor; ?></th>
<th><a href="index.php?sorting='.$sort.&field=Title"><?php echo $sortTitle; ?></th>
<th></th><th></th>
</tr>
<?php