php - A switch between ascending and descending table rows on a tableheader click -
i've been messing quite time now, can't work. i'm trying make ;
a clickable table header, once clicked switches row order ascending descending , when clicked again.
the attempts did far didn't loop, , got stuck after 1 click.
just add link table header cell contains parameter, e.g.
<tr><th> <a href="currentpage.php?order=<?php echo isset($_get['order'])?!$_get['order']:1; ?>"> name </a> </th></tr>
what happens here? link added, containing order
parameter set opposite of current order
value(1/true or 0/false) or 1 default.
in php script can decide how order table using order
value:
$isasc = isset($_get['order'])? (bool) $_get['order']: 1;
now can use $isasc
boolean:
if ($isasc) { // sort data ascending } else { // sort data descending }
or in query:
$sql = "select * tabe order name ".($isasc?"asc":"desc").";";
of course, can extend idea, e.g., adding column names sort several columns.
Comments
Post a Comment