使用MySQL和PHP创建过滤功能

时间:2013-02-12 01:16:02

标签: php mysql

嘿这一切都是我的问题,我正在使用SQL查询尝试根据帖子的元数据返回结果。在用户界面内的帖子创建期间,用户输入该元数据。我有一个过滤器的问题,它一次只适用于一个变量,但不超过它(例如:如果选择主题它工作正常,但是如果选择主题AND媒体它将不返回任何结果)这里是代码:

$db_build_post_filter_WHERE = array();
// Default to avoid errors on WHERE GROUP BY
$db_build_post_filter_WHERE [] = 'cmsp_post.post_id > 0';
//$db_build_post_filter_WHERE [] = 'cmsp_post.post_id = cmsp_post_meta.post_id';
if ( isset( $gnocore_cmsp_build_topic_slug_id_array[$filter_topic] ) ) {
    $db_build_post_filter_WHERE [] = 'post_meta_key = "topic_id" AND post_meta_value = ' . $gnocore_cmsp_build_topic_slug_id_array[$filter_topic];
}
if ( isset( $gnocore_cmsp_build_media_slug_id_array[$filter_media] ) ) {
$db_build_post_filter_WHERE [] = 'post_meta_key = "media_id" AND post_meta_value = ' . $gnocore_cmsp_build_media_slug_id_array[$filter_media];
}
if ( isset( $gnocore_cmsp_build_author_slug_id_array[$filter_author] ) ) {
$db_build_post_filter_WHERE [] = 'post_meta_key = "author_id" AND post_meta_value = ' . $gnocore_cmsp_build_author_slug_id_array[$filter_author];
}   

// PROJECT FILTER ARRAY 
$build_post_filter_array = array();
gnoshare_db_select ('cmsp_post LEFT JOIN cmsp_post_meta ON cmsp_post.post_id = cmsp_post_meta.post_id','cmsp_post.post_id',implode(' AND ',$db_build_post_filter_WHERE) . ' GROUP BY cmsp_post.post_id','cmsp_post.project_id, post_name, cmsp_post.post_id','db_build_post_filter_array_num','db_build_post_filter_array_results');
if ( $db_build_post_filter_array_num > 0 ) {
    foreach ($db_build_post_filter_array_results as $db_build_post_filter_array_result) {
        $build_post_filter_array [$db_build_post_filter_array_result->post_id] = '';
    }
}

我相信我的问题出现在“PROJECT FILTER ARRAY”部分,如果有人可以提供帮助,我将非常感激。

干杯

编辑:将'AND'更改为'OR'会产生一个简单显示所有帖子的结果,我认为可以公平地说我的问题在于这行代码,但我仍然可以找不到生成我正在寻找的结果的方法。

gnoshare_db_select ('cmsp_post LEFT JOIN cmsp_post_meta ON cmsp_post.post_id = cmsp_post_meta.post_id','cmsp_post.post_id',implode(' OR ',$db_build_post_filter_WHERE) . ' GROUP BY cmsp_post.post_id','cmsp_post.project_id, post_name, cmsp_post.post_id','db_build_post_filter_array_num','db_build_post_filter_array_results');

编辑:这是我的表(大致):

+----------+---------------+-----------------+  
| post_id  | post_meta_key | post_meta_value |  
+----------+---------------+-----------------+  
|     1    |   topic_id    |        1        |  
+----------+---------------+-----------------+  
|     1    |   media_id    |        1        |  
+----------+---------------+-----------------+  
|     1    |   author_id   |        2        |  
+----------+---------------+-----------------+  
|     2    |   media_id    |        2        |  
+----------+---------------+-----------------+  
|     2    |   topic_id    |        2        |  
+----------+---------------+-----------------+  

我希望它根据用户为帖子做出的选择进行过滤(例如:用户选择的主题1,媒体1和作者2用于帖子1和媒体2,主题2用于帖子2 显示在上面的表)鉴于此示例为true,我希望我的网页生成一个帖子,如果过滤器选择了以下任何一个:过滤器按主题1将仅显示一个帖子,按主题1和媒体1过滤将仅显示帖子1,按主题1过滤媒体2将显示一条消息,指出所选标准没有匹配,依此类推。这会澄清一点吗?

1 个答案:

答案 0 :(得分:0)

如果我没错,你应该在过滤器之间使用OR并用()包围它们。类似的东西:

 $db_build_post_filter_WHERE [] = '(cmsp_post.post_id > 0)';
 if ( isset( $gnocore_cmsp_build_topic_slug_id_array[$filter_topic] ) ) {
   $db_build_post_filter_WHERE [] = '(post_meta_key = "topic_id" 
                                     AND post_meta_value = ' . 
                $gnocore_cmsp_build_topic_slug_id_array[$filter_topic].')';
 }
 if ( isset( $gnocore_cmsp_build_media_slug_id_array[$filter_media] ) ) {
   $db_build_post_filter_WHERE [] = '(post_meta_key = "media_id" 
                                    AND post_meta_value = ' . 
               $gnocore_cmsp_build_media_slug_id_array[$filter_media].')';
 }
 //Other conditions

// PROJECT FILTER ARRAY 
$build_post_filter_array = array();
gnoare_db_select ('cmsp_post 
                   LEFT JOIN cmsp_post_meta
                          ON cmsp_post.post_id = cmsp_post_meta.post_id',
                 'cmsp_post_post.post_id',
                 implode(' OR ',$db_build_post_filter_WHERE) . 
                ' GROUP BY cmsp_post.post_id','cmsp_post.project_id, post_name, cmsp_post.post_id','db_build_post_filter_array_num','db_build_post_filter_array_results');