在表头上的升序表行和降序表行之间切换,单击 [英] A switch between ascending and descending table rows on a tableheader click
问题描述
我已经对此困扰了很长时间,但是我无法使其正常工作.我想做的是;
I've been messing with this for quite some time now, but I just can't get it to work. What I'm trying to make is ;
可点击的表头,一旦被点击,则将行顺序从升序转换为降序,并在再次单击时返回.
A clickable table header, that once clicked switches the row order from ascending to descending and back when clicked again.
我到目前为止所做的尝试没有循环,一键后卡住了.
The attempts I did so far didn't loop, and got stuck after one click.
推荐答案
只需将链接添加到表头单元格,其中包含一个参数,例如
Just add a link to your table header cell which contains a parameter, e.g.
<tr><th>
<a href="currentpage.php?order=<?php echo isset($_GET['order'])?!$_GET['order']:1; ?>">
Name
</a>
</th></tr>
这里发生了什么?将添加一个链接,其中包含一个order
参数,该参数设置为当前order
值的相反值(1/true或0/false)或默认为1.
What happens here? A link will be added, containing an order
parameter which is set to the opposite of the current order
value(1/true or 0/false) or to 1 as default.
在您的PHP脚本中,您现在可以决定如何使用order
值对表进行排序:
In your PHP script you can now decide how to order your table using the order
value:
$isAsc = isset($_GET['order'])? (bool) $_GET['order']: 1;
现在您可以使用$isAsc
布尔值:
Now you can use the $isAsc
boolean:
if ($isAsc) {
// Sort data ascending
} else {
// Sort data descending
}
或在查询中:
$sql = "SELECT * FROM tabe ORDER BY name ".($isAsc?"ASC":"DESC").";";
当然,您可以扩展此想法,例如,通过添加列名来按几列进行排序.
Of course, you can extend this idea, e.g., by adding column names to sort by several columns.
这篇关于在表头上的升序表行和降序表行之间切换,单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!