Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:以智能编码方式进行分类?_Php_Mysql_Sorting_Filtering - Fatal编程技术网

PHP:以智能编码方式进行分类?

PHP:以智能编码方式进行分类?,php,mysql,sorting,filtering,Php,Mysql,Sorting,Filtering,所以我有这个视频部分,但我想为用户提供一个“排序”选项 排序选项:ALL(在sql语句中没有WHERE)、Videoklips(其中SCtry=0)、SC-try(其中SCtry=1) 现在,我知道怎么用“我的”方式做了。我会在index.php上放置链接: ?sort=video和?sort=SCtry 然后制作2,如果排序视频,如果排序sctry 然后立即将整个index.php(显示所有内容)复制到2 if中,然后编辑SQL语句SELECT,其中SCtry='0'on?sort=video

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

排序选项:ALL(在sql语句中没有WHERE)、Videoklips(其中SCtry=0)、SC-try(其中SCtry=1)

现在,我知道怎么用“我的”方式做了。我会在index.php上放置链接:
?sort=video
?sort=SCtry

然后制作2,如果排序视频,如果排序sctry 然后立即将整个index.php(显示所有内容)复制到2 if中,然后编辑SQL语句SELECT,其中SCtry='0'on?sort=video,其中SCtry='1'on?sort=SCtry

现在,我知道如何分类,但我想用一种更智能的方式编码它(当然,如果它存在的话),因为这似乎太多了,无法复制整个站点,然后只更改一行

我将复制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请求编辑…

几个问题:您没有使用smarty模板引擎,是吗?如果正确,请移除smarty标签。下一个复制index.php的确切含义是什么?你能给出一个你想要复制的代码的简单示例吗?请发布一些示例代码,这样我们就可以给你展示一个相同代码的反例。好的,可以,不,我不使用smarty模板,我现在要删除标签谢谢你Azzyh。我认为迈克的回答总结得很好。不需要复制index.php.Yes这太棒了,尽管我不知道如果我想在$sql行中按id排序该怎么办。。如果我这样做,那么分类就不起作用了,因为ORDER BY出现在“WHERE”之前,这正是我想要的。。但是如果我也有orderbyid,我该怎么办呢?我会选择像$sort_options=array(“video”=>“where
SCtry
=0”…);if(设置($sort\u options[$\u GET['sort']])$sql.=$sort\u option[$\u GET['sort']];但我同意,不管怎样,使用诸如条令或zend_db_表之类的东西会更好。然而,这个问题的本质是如此的基本,我想根据OP能够理解和使用的代码来定制答案。