doupapin172773 2014-04-04 16:59
浏览 8
已采纳

为不同的PHP ORDER BY语句创建一个新页面?

I have a PHP script that outputs a list of reviews in a table and orders them by the date they were submitted - (similar to have Trip Advisor ratings work).

Below I have included how the table heading are set out:

NAME || DATE || REVIEW || RATING

Below I have listed the code:

//run a query to find all the fields in the review table that belong to the specific hall, using the id in the url ($current_id)
if ($r = $db->prepare("SELECT * FROM reviews WHERE hall_id = :current_id ORDER BY overall DESC")) {
    //bind the parameters used in the above query using the 'current_id' variable
    $r->bindParam(':current_id', $current_id);
    //Execute the prepared query
    $r->execute(); 

        //search review table for all fields and save them in $review
        $reviewtemp = $r->fetchAll();
       foreach( $reviewtemp as $review) { ...

ORDER BY date DESC orders the reviews by the date. However, when the user clicks on the 'RATING' header in the table, I need the reviews to be ordered by the highest rating (which is a number). So ORDER BY date DESC would change into ORDER BY rating DESC.

I'm unsure whether I have to create an entire new page (and simply change 1 word of my php script) to do this, or if there is a more simple and efficient method?

  • 写回答

2条回答 默认 最新

  • dqyin0101 2014-04-04 17:06
    关注

    I wouldn't make whole new page, I would say have a variable that you can change that you would use in the order by in your query.

    $orderby = 'rating';
    

    And then your query would have "ORDER BY $orderby DESC"

    EDIT

    If you make the date header a link to "yourscript.php?orderby=date", you could have something like this on "yourscript.php"

    switch($_GET['orderby']){
        case 'date':
            $orderby = 'date';
            break;
        default :
            $orderby = 'rating';
            break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?