我正在使用一些过滤器来显示产品。滤色器,如颜色,价格和东西。
链接:http://www.applechain.com/products/iPod.php
我使用此查询
$sql = "Select *
from tbl_product
where device='iPhone'
and (color='$c1'
or color='$c2'
or color='$c3'
or color='$c4'
or color='$c5'
or color='$c6'
or color='$c7'
or color='$c8'
or color='$c9'
or color='$c10'
) and (storage='$cp1'
or storage='$cp2'
or storage='$cp3'
or storage='$cp4'
or storage='$cp5'
) and (f_unlock='$factory')
and (warranty='$warranty')
and (price >= '$price1'
and price <= '$price2'
)
order by product_id desc";
我正在使用AND条件作为主要参数。现在,如果只满足两个参数,我该如何显示结果。如何实现,如果颜色和存储参数得到满足,它只显示基于它们的结果,而不管其他是否被选中。
答案 0 :(得分:0)
简单。从查询中删除其他条件。
顺便说一句,你可以这样重写那个查询...SELECT *
FROM tbl_product
WHERE device = 'iPhone'
AND color IN ('$c1','$c2','$c3','$c4','$c5','$c6','$c7','$c8','$c9','$c10')
AND storage IN ('$cp1','$cp2','$cp3','$cp4','$cp5')
AND f_unlock = '$factory'
AND warranty = '$warranty'
AND price BETWEEN '$price1' and '$price2'
ORDER
BY product_id DESC;
答案 1 :(得分:0)
您可以根据设置的内容构建查询,通常ORM工具会为您执行此操作。
由于开始使用PHP框架可能为时已晚,您应该执行以下操作:
if(isset($colours))
{
$sub_query = concatenateOrClauses($colours, 'color');
$main_query .= $sub_query;
}
private function concatenateOrClauses($values, $name)
{
$query_string = "";
for($i = 0; $i < sizeof($values); $i++)
{
if($i == 0)
{
$query_string = $name."=".$values[$i];
}
else
{
$query_string = $query_string." OR ".$name."=".$values[$i];
}
}
return $query_string;
}