嘿,這裏是我的問題,我正在使用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