4

嘿,这是我的问题,我正在使用 SQL 查询尝试根据帖子的元数据返回结果。该元数据由用户在用户界面内创建帖子期间输入。我遇到了过滤器问题,它一次只能处理一个变量,但仅此而已(例如:如果选择了主题,它可以正常工作,但是如果选择了主题和媒体,它将不返回任何结果)这里是编码:

$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] = '';
    }
}

我相信我的问题出在“项目过滤器阵列”部分,如果有人能提供帮助,将不胜感激。

干杯

编辑:将“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 过滤,媒体 1 将仅显示帖子 1,按主题 1 过滤媒体 2 将显示一条消息,指出与所选标准不匹配,依此类推。这是否更清楚一点?

4

1 回答 1

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');
于 2013-02-12T06:48:40.357 回答