所以我有这个视频部分,但我想为用户提供一个“排序”选项。
排序选项:ALL(sql 语句中无 WHERE)、Videoklips (WHERE SCtry = 0)、SC try (WHERE SCtry = 1)
现在,我知道如何以“我的”方式去做。我会在index.php上放置链接:
?sort=video
和 ?sort=SCtry
然后做2,如果排序视频,如果排序sctry 然后立即将整个index.php(显示所有内容)复制到2个if中,然后编辑SQL语句SELECT,其中WHERE SCtry = '0' on ?sort=video,WHERE SCtry = '1' on ?sort=video排序=SCtry。
现在,我知道如何排序,但我想以更智能的方式对其进行编码(当然,如果它存在的话),因为这似乎太多了,无法复制整个网站,然后只更改 1 行...
我要复制的 index.php 的示例:
<?php
$hent_meetup = mysql_query("SELECT * FROM member_film ORDER BY id DESC LIMIT 0,200") or die(mysql_error());
while($vis = mysql_Fetch_array($hent_meetup)) {
?>
最佳答案
在没有看到示例代码的情况下,我可以告诉你这是我要做的事情的一个示例。
<?
//all of the code before the SQL statement here...
$sql= ' SELECT `column` from `tablename`'; //or any other SQL that is appropriate before the conditional
if(isset($_GET['sort'])){
if($_GET['sort'] == 'video'){
$sql .= ' WHERE `SCtry` = 0';
}elseif($_GET['sort'] == 'SCtry'){
$sql .= ' WHERE `SCtry` = 1';
}
}
$sql .= ' ORDER BY `whatever`'; //or any other SQL that is appropriate after the conditional
//rest of code... no need for duplication
?>
根据OP请求进行编辑...
关于PHP:以智能编码方式进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2347833/