这是我的观点页面:
<?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 等......
答案 0 :(得分:1)
$_GET['order']
)in_array(strtolower($_GET['order']), ['asc', 'desc'])
答案 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);