嘿,这里是我的问题,我正在使用SQL查询来尝试返回基于帖子元数据的结果。这个元数据是用户在用户界面创建后创建的。我遇到了一个过滤器问题,它一次只能处理一个变量,但不超过这个范围(例如:如果主题被选中,它可以正常工作,但是如果选择了主题AND介质,它将不返回结果),这里是代码:使用MySQL和PHP创建过滤功能
$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”部分,如果有人可以协助它,将不胜感激。
干杯
编辑:改变“和” to“或”生成的简单显示所有帖子的结果,我认为这是公平地说,我的问题就出在这行代码,但是我仍然找不到一种方法来生成我正在寻找的结果。
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的帖子2 (如上表))假设这个例子是真的,我希望我的网页生成后一个,如果任何以下内容被过滤器选中:按主题过滤1仅显示帖子1,按主题1过滤,媒体1仅显示帖子1,按主题过滤1媒体2将显示一条消息,指出与所选条件没有匹配,依此类推。这是否澄清了一点?
有没有办法用这个gnoshare_db_select()函数来打印正在使用的最终查询? – Levi 2013-02-12 03:28:57
[查询] => SELECT cmsp_post.post_id FROM cmsp_post LEFT JOIN cmsp_post_meta ON cmsp_post.post_id = cmsp_post_meta.post_id WHERE cmsp_post.post_id> 0 AND post_meta_key = “topic_id” AND post_meta_value = 1 GROUP BY cmsp_post.post_id ORDER BY cmsp_post.project_id, POST_NAME,cmsp_post.post_id [RETURN_VAL] – user2063199 2013-02-12 04:03:07