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


Create a new page for different php ORDER BY statement?

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

下面我列出了表格标题的列出方式:

姓名 ||日期 ||评论 ||额定值

下面我列出了代码:

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

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

我不会制作全新的页面,我会说有一个变量,您可以更改该变量,您将在查询中按顺序使用。

$orderby = 'rating';

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

编辑

如果你把日期标题作为"yourscript.php?orderby=date"的链接,你可以在"yourscript.php上有这样的东西。

switch($_GET['orderby']){
    case 'date':
        $orderby = 'date';
        break;
    default :
        $orderby = 'rating';
        break;
}

由你决定,但是当我这样做时,我使用tablesorter。看看,它可以让您显示结果并单击标题即时重新排序,没有新查询。它还允许您通过按住 shift 按多列排序。

我捡了一把小提琴让你看看。

http://tablesorter.com