doutong1890 2018-08-13 12:49
浏览 94
已采纳

ASC DESC在同一链接中

if (isset($_GET['order']) && $_GET['order'] == 'category')
{

    $sql .= " ORDER BY category ".$_GET["direction"];
}

<a href='?order=category&direction=ASC'>

When the user clicks second time on the link I want to order the table in DESC order. I have the a-z order I need to reverse them when the user clicks on the same link.

  • 写回答

2条回答 默认 最新

  • dtpf76658 2018-08-13 14:03
    关注

    Simply check what is contained in the $_GET['direction'] and reverse it each time the submit is run.

    Also, to take care of page reloads, save the value of previous direction in session.

    $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;?>>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题