使用链接更改内容ORDERING BY DESC,ASC和e.t.c.相似的页面

时间:2014-10-26 15:49:09

标签: php mysql database hyperlink sql-order-by

这是我的观点页面:

<?php

include 'connect/con.php';

$result = mysqli_query($con,"SELECT newsvid.id, newsvid.vidTitle, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id ORDER BY newsvid.id DESC");

while($row = mysqli_fetch_array($result)) {
  echo '<div class="id">#<a href="details.php?id='.$row['id'].'">'.$row['id'].'</a></div>';
  echo "<div class=\"vidTitle\">" . $row['vidTitle'] . "</div>";
  echo "<div class=\"imgCover\"><img class=\"imageCover\"src=\"" . $row['url'] . "\"></div>";
  echo "<div class=\"vidSD\">" . $row['vidSD'] . "</div>";
  echo "<div class=\"vidDetails\"> 
<table class=\"tableSD\" >
<tr><td class=\"tdBR\"><strong> Years: </strong></td><td class=\"tdB\">" . $row['vidYear'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> City: </strong></td><td class=\"tdB\">". $row['vidCity'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Zanr: </strong></td><td class=\"tdB\">". $row['vidZanr'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Quality: </strong></td><td class=\"tdB\">". $row['vidQuality'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Translated: </strong></td><td class=\"tdB\">". $row['vidTranslated'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Video time: </strong></td><td class=\"tdB\">". $row['vidTime'] .  "</td></tr>
</table> 
 </div>";


  echo "<div class=\"published\"><strong>Published: </strong>" . $row['published'] . "</div>"; 
}
mysqli_close($con);
?>

我需要在index.php页面中使用 LINK 这一位&#34; ORDER BY newsvid.id DESC&#34;将根据点击的链接进行更改。

EG:

<div class="mainLeftCover">
<a href="#>ASC</a> |
<a href="#>DESC</a> |
</div>

如果我按ASC,view.php页面将显示结果 ORDERED BY newsvid.id ASC ,如果我将按DESC,则view.php页面将显示结果 ORDERED BY newsvid。 id DESC 等......

2 个答案:

答案 0 :(得分:1)

  • 制作两个以ASC和DESC为主题的链接作为网址查询参数(例如$_GET['order']
  • 在服务器端,请务必检查该值是ASC还是DESC以防止恶意(即in_array(strtolower($_GET['order']), ['asc', 'desc'])
  • 将参数附加到查询,以便DESC / ASC部分是可变的

答案 1 :(得分:1)

首先,您需要在链接网址

中添加参数
<div class="mainLeftCover">
<a href="view.php?order=ASC">ASC</a> 
<a href="view.php?order=DESC">DESC</a> 
</div>

然后使用查询的存根减去ORDER部分设置变量。

$query ="SELECT newsvid.id, newsvid.vidTitle, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id";

使用isset()ternary operator来获取参数,或者如果未设置,则传递默认值。

$order = isset($_GET['order']) ? $_GET['order'] : 'DESC';//Change to ASC if you want this as default

使用 in_array()进行清理

$goodParam = array("ASC", "DESC");
if (in_array($order, $goodParam)) {

然后使用.= Concatenation assignment构建$ query。

        if($order == 'DESC'){
            $query .= " ORDER BY newsvid.id DESC";
        }else{
            $query .= " ORDER BY newsvid.id ASC";
      }
}
    $result = mysqli_query($con,$query);