user1658170 user1658170 - 1 year ago 70
PHP Question

Reverse search order by clicking on table header

I have a table that displays from MYSQL in an ordered fashion. I am trying to get it so that if I click the header link it will reverse the order and then if I click it again it will revert to the original case. I am getting stuck on trying to revert it. Here is what I have so far:


case "asc":
$orderBy = "ORDER BY whole_date asc";

case "desc":
$orderBy = "ORDER BY whole_date desc";

$orderBy = "ORDER BY whole_date desc";

Referred to in the the SQL statement here:

...WHERE user_id = '$username'".$orderBy." LIMIT $offset, $rowsperpage ";

In my table header I have the link like this in HTML:

<th><a href='view.php?sort=desc'>header</a></th>

This is where my problem is, how would I get it to revert back. How would I include a sort=asc link?

Answer Source
printf('<a href="view.php?sort=%s">header</a>',
       empty($_GET['sort']) || $_GET['sort'] == 'asc' ? 'desc' : 'asc');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download