PHP:以智能编码方式进行排序?

标签 php mysql sorting filtering

所以我有这个视频部分,但我想为用户提供一个“排序”选项。

排序选项: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/

相关文章:

php - 如果未指定 mysql 链接,mysql 函数是否会打开一个新的 mysql 连接?

php - 不从安装在 crontab 中的 shell 脚本调用 php 脚本

PHP棘手问题

php - 在 PHP 中将数据从 SQL 转换为字符串

java - 当 ArrayList 排序时,DefaultListModel 不会改变

matlab - 从右到左对连接的组件进行排序

php - 函数 eregi() 已弃用

MySQL 将单列数据拆分为两列

mysql - 错误 1241 操作数应包含 2 列

Javascript - 如何按 3 种不同的属性类型对对象数组进行排序? (字符串、整数、 bool 值)