为不同的 php ORDER BY 语句创建一个新页面? [英] Create a new page for different php ORDER BY statement?

查看:41
本文介绍了为不同的 php ORDER BY 语句创建一个新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 脚本,可以在表格中输出评论列表,并按提交日期对它们进行排序 -(类似于使用 Trip Advisor 评级).

下面我介绍了表格标题的排列方式:

姓名 ||日期 ||评论 ||评分

下面我列出了代码:

//使用url中的id($current_id)运行查询查找review表中属于特定大厅的所有字段if ($r = $db->prepare("SELECT * FROM review WHEREhall_id = :current_id ORDER BY整体 DESC")) {//使用'current_id'变量绑定上面查询中使用的参数$r->bindParam(':current_id', $current_id);//执行准备好的查询$r->execute();//搜索所有字段的评论表并将它们保存在$review中$reviewtemp = $r->fetchAll();foreach( $reviewtemp as $review) { ...

ORDER BY date DESC 按日期对评论进行排序.但是,当用户单击表中的RATING"标题时,我需要按最高评级(这是一个数字)对评论进行排序.所以ORDER BY date DESC会变成ORDER BY rating DESC.

我不确定是否必须创建一个完整的新页面(并且只需更改我的 php 脚本的 1 个单词)才能执行此操作,或者是否有更简单有效的方法?

解决方案

我不会制作全新的页面,我会说有一个变量,您可以更改它,您将在查询中的 order by 中使用该变量.

>

$orderby = '评级';

然后你的查询会有ORDER BY $orderby DESC"

编辑

如果您将日期标题设为指向yourscript.php?orderby=date"的链接,则您可以在yourscript.php"上添加类似内容

switch($_GET['orderby']){案例日期":$orderby = '日期';休息;默认 :$orderby = '评级';休息;}

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?

解决方案

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;
}

这篇关于为不同的 php ORDER BY 语句创建一个新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆