du997562 2014-07-27 11:32
浏览 86
已采纳

使用PHP和GET方法排序?

I'm trying to make a sorting, using links and GET method with them. I'll provide the code first and then explain what I'm trying to achieve. There could be a better way by using AJAX, and making it dynamic (I think?), but I want to start off with this first.

Code:

if(isset($_GET['order'])){$order = $_GET['order'];}else{$order = 0;}
if(isset($_GET['field'])){$order_field = $_GET['field'];}

switch($order)
{
    case 0: 
        $order_next = "DESC";
        break;
    case "DESC":
        $order_next = "ASC";
        break;
    case "ASC":
        $order_next = 0;
        break;
}

And later, in the HTML page I have this snippet:

<? 
if($order == 0)
{
    printf("<a href='admin.php?field=lastname&order=%s'>Last</a>
    ", $order_next);
}
else
{
    printf("<a href='admin.php?field=%s&order=%s'>Last</a><a class='sort_desc' href='admin.php?field=%s&order=%s'></a>
    ", $order_field, $order_next, $order_field, $order_next);
}
?>

Okay. I have a link Last, which should have 3 positions, default, DESC order, ASC order.
When I click on it once, it should send via GET method, that the field I clicked was "Lastname" field, and I need the next position (from default (without sorting) to next one (DESC in this case)). I can't quite catch the error on the code, or I don't understand something, doesn't the page reload properly or something? When I click the link, the $order and $order_field is read from the GET method, but nothing else happens. The switch doesn't work for some reason, and $order_next doesn't change into the new value, and there is only 1 link, instead of 2:
Default: Link "Last" DESC: Link "Last" + link with the triangle picture pointing down.

P.S: Sorry if I might not explain this too well, but I tried. Thank you for your time!

  • 写回答

3条回答 默认 最新

  • dqrfdl5708 2014-07-27 11:43
    关注

    Change case 0: to case "0": and all will be good with the world :-)

    This is because switch uses Loose Comparison

    To understand why, just run this code and see the result:

    var_dump("DESC" == 0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大