我有一个主要的困境。我有一个搜索,根据用户搜索或用户点击类别填充搜索。进行搜索或单击业务或领导等类别时,将显示子类别。
我希望发生以下行为: 单击特定子类别时,会减少结果量。就像让我们说结果是100,并且你点击html,它将是10,但是让我们说你检查了多个子类别,然后子类别被加起来所以说html有10个课程,然后css 30,你点击将显示40以上。
在当前状态下,它没有组合它们,而只考虑最后一个复选框,例如,如果检查了html然后css,只有在两者都应该考虑时它才会看css。
以下是涵盖子类别的部分:
//sub categories
$sub1Arr = array();
if (!empty($category1)) $sub1Arr[] = $category1;
if (count($sub1Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc1 IN ('".implode("','", $sub1Arr)."')) ";
}
$sub2Arr = array();
if (!empty($category2)) $sub2Arr[] = $category2;
if (count($sub2Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc2 IN ('".implode("','", $sub2Arr)."')) ";
}
$sub3Arr = array();
if (!empty($category3)) $sub3Arr[] = $category3;
if (count($sub3Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc3 IN ('".implode("','", $sub3Arr)."')) ";
}
$sub4Arr = array();
if (!empty($category4)) $sub4Arr[] = $category4;
if (count($sub4Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc4 IN ('".implode("','", $sub4Arr)."')) ";
}
$sub5Arr = array();
if (!empty($category5)) $sub5Arr[] = $category5;
if (count($sub5Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc5 IN ('".implode("','", $sub5Arr)."')) ";
}
以下是代码的更大部分:
$get_crs_mysqli_count = "
SELECT *
FROM courses
WHERE course_date1 >= CURRENT_DATE() ";
if(!empty($_SESSION['userSearch']))
{
$get_crs_mysqli_count .= " AND (course_title like '%" . $_SESSION['userSearch'] . "%') ";
}
elseif(!empty($_SESSION['userCategory']))
{
$get_crs_mysqli_count .= " AND ((course_cat1 like '%" . $_SESSION['userCategory'] . "%') OR (course_cat2 like '%" . $_SESSION['userCategory'] . "%')) ";
}
elseif(!empty($_SESSION['userDate']))
{
$get_crs_mysqli_count .= " AND ((course_date1 like '%" . $_SESSION['userDate'] . "%') OR (course_date2 like '%" . $_SESSION['userDate'] . "%')) ";
}
elseif(!empty($_SESSION['userProvider']))
{
$get_crs_mysqli_count .= " AND (course_provider like '%" . $_SESSION['userProvider'] . "%') ";
}
elseif(!empty($_SESSION['userCity']))
{
$get_crs_mysqli_count .= " AND (course_city like '%" . $_SESSION['userCity'] . "%') ";
}
elseif(!empty($_SESSION['userPrice']))
{
$get_crs_mysqli_count .= " AND (course_price like '%" . $_SESSION['userPrice'] . "%') ";
}
elseif(!empty($_SESSION['userSub']))
{
$get_crs_mysqli_count .= " AND ((course_subc1 like '%" . $_SESSION['userSub'] . "%') OR (course_subc2 like '%" . $_SESSION['userSub'] . "%') or (course_subc3 like '%" . $_SESSION['userSub'] . "%') or (course_subc4 like '%" . $_SESSION['userSub'] . "%') or (course_subc5 like '%" . $_SESSION['userSub'] . "%')) ";
}
$durationArr = array();
if (!empty($duration1)) $durationArr[] = $duration1;
if (!empty($duration2)) $durationArr[] = $duration2;
if (!empty($duration3)) $durationArr[] = $duration3;
if (!empty($duration4)) $durationArr[] = $duration4;
if (!empty($duration5)) $durationArr[] = $duration5;
if (count($durationArr)>0) {
$get_crs_mysqli_count .= " AND (course_duration IN ('".implode("','", $durationArr)."')) ";
}
//From Price
$fromPriceArr = array();
if (!empty($fromPrice)) $fromPriceArr[] = $fromPrice;
if (count($fromPriceArr)>0) {
$get_crs_mysqli_count .= " AND (course_priceFinal >= ('".implode("','", $fromPriceArr)."')) ";
}
//To Price
$toPriceArr = array();
if (!empty($toPrice)) $toPriceArr[] = $toPrice;
if (count($toPriceArr)>0) {
$get_crs_mysqli_count .= " AND (course_priceFinal <= ('".implode("','", $toPriceArr)."')) ";
}
//start date
$startArr = array();
if (!empty($startDate)) $startArr[] = $startDate;
if (count($startArr)>0) {
$get_crs_mysqli_count .= " AND (course_date1 >= ('".implode("','", $startArr)."')) ";
}
//end date
$endArr = array();
if (!empty($endDate)) $endArr[] = $endDate;
if (count($endArr)>0) {
$get_crs_mysqli_count .= " AND (course_date1 <= ('".implode("','", $endArr)."')) ";
}
//sub categories
$sub1Arr = array();
if (!empty($category1)) $sub1Arr[] = $category1;
if (count($sub1Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc1 IN ('".implode("','", $sub1Arr)."')) ";
}
$sub2Arr = array();
if (!empty($category2)) $sub2Arr[] = $category2;
if (count($sub2Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc2 IN ('".implode("','", $sub2Arr)."')) ";
}
$sub3Arr = array();
if (!empty($category3)) $sub3Arr[] = $category3;
if (count($sub3Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc3 IN ('".implode("','", $sub3Arr)."')) ";
}
$sub4Arr = array();
if (!empty($category4)) $sub4Arr[] = $category4;
if (count($sub4Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc4 IN ('".implode("','", $sub4Arr)."')) ";
}
$sub5Arr = array();
if (!empty($category5)) $sub5Arr[] = $category5;
if (count($sub5Arr)>0) {
$get_crs_mysqli_count .= " AND (course_subc5 IN ('".implode("','", $sub5Arr)."')) ";
}
//city
$cityArr = array();
if (!empty($city)) $cityArr[] = $city;
if (count($cityArr)>0) {
$get_crs_mysqli_count .= " AND (course_city IN ('".implode("','", $cityArr)."')) ";
}
//delivery
$delivery1Arr = array();
if (!empty($delivery1)) $delivery1Arr[] = $delivery1;
if (count($delivery1Arr)>0) {
$get_crs_mysqli_count .= " AND (course_delivery IN ('".implode("','", $delivery1Arr)."')) ";
}
$delivery2Arr = array();
if (!empty($delivery2)) $delivery2Arr[] = $delivery2;
if (count($delivery2Arr)>0) {
$get_crs_mysqli_count .= " AND (course_delivery2 IN ('".implode("','", $delivery2Arr)."')) ";
}
//provider
$providerArr = array();
if (!empty($provider1)) $providerArr[] = $provider1;
if (!empty($provider2)) $providerArr[] = $provider2;
if (!empty($provider3)) $providerArr[] = $provider3;
if (!empty($provider4)) $providerArr[] = $provider4;
if (!empty($provider5)) $providerArr[] = $provider5;
if (count($providerArr)>0) {
$get_crs_mysqli_count .= " AND (course_provider IN ('".implode("','", $providerArr)."')) ";
}
$guaranteeArr = array();
if (!empty($guarantee1)) $guaranteeArr[] = $guarantee1;
if (!empty($guarantee2)) $guaranteeArr[] = $guarantee2;
if (count($guaranteeArr)>0) {
$get_crs_mysqli_count.= " AND (course_guarantee IN ('".implode("','", $guaranteeArr)."')) ";
}
$get_crs_mysqli_count .= "
ORDER BY course_date1 ASC ";
请注意,子类别与其他过滤器协作工作至关重要。
更新
//sub categories
$sub1Arr = array();
$sub2Arr = array();
$sub3Arr = array();
$sub4Arr = array();
$sub5Arr = array();
if (!empty($category1)) $sub1Arr[] = $category1;
if (!empty($category2)) $sub2Arr[] = $category2;
if (!empty($category3)) $sub3Arr[] = $category3;
if (!empty($category4)) $sub4Arr[] = $category4;
if (!empty($category5)) $sub5Arr[] = $category5;
if (count($sub1Arr)>0) {
$get_crs_mysqli_count .= " AND ((course_subc1 IN ('".implode("','", $sub1Arr)."')) OR (course_subc2 IN ('".implode("','", $sub2Arr)."'))
or (course_subc3 IN ('".implode("','", $sub3Arr)."')) or (course_subc4 IN ('".implode("','", $sub4Arr)."')) or (course_subc5 IN ('".implode("','", $sub5Arr)."')))";
}
答案 0 :(得分:1)
如果要合并多个类别,则不得使用AND
。请改用OR
。它与您用于子类别的IN
几乎相同。
基本上,您的查询应如下所示:
SELECT * FROM courses WHERE course_date1 >= CURRENT_DATE() AND (course_title like '%<something>%') AND (...) AND (
(course_subc1 IN (<sub1Arr>) OR course_subc2 IN (<sub2Arr>) OR (...)
)
请注意类别之间的OR
...