if (isset($_GET['order']) && $_GET['order'] == 'category')
{
$sql .= " ORDER BY category ".$_GET["direction"];
}
<a href='?order=category&direction=ASC'>
当用户第二次单击链接时,我要按DESC顺序对表进行排序。当用户单击相同的链接时,我需要按a-z顺序将其撤消。
答案 0 :(得分:0)
您可以切换会话/ cookie中的单击状态。
if (isset($_GET['order']) && $_GET['order'] == 'category') {
$sql .= " ORDER BY category ".$_GET["direction"];
$dir = $_GET['direction'] == 'ASC' ? 'DESC' : 'ASC';
}
if($_SESSION['click'] == 0){
echo "<a href='?order=category&direction=ASC'>";
} else{
echo "<a href='?order=category&direction=DESC'>";
}
答案 1 :(得分:0)
只需检查$_GET['direction']
中包含的内容,并在每次运行提交时将其反转。
另外,要照顾页面重新加载,请在会话中保存前一个方向的值。
$dir = 'DESC'; // set default for first execution
@session_start(); // start the session if it isn't already started.
if(!empty($_GET['direction'])){
$dir = $_GET['direction'] == 'ASC' ? 'ASC' : 'DESC'; // avoids SQL injection.
$_SESSION['direction'] = $dir;
}elseif(!empty($_SESSION['direction'])){
$dir = $_SESSION['direction'];
}
if (isset($_GET['order']) && $_GET['order'] == 'category') {
$sql .= " ORDER BY category ".$dir;
}
$reverseDir= $dir =='DESC' ? 'ASC' : 'DESC'; //reverses the direction to be used in the link
<a href='?order=category&direction='<?php echo $reverseDir;?>>