我正在尝试创建多级搜索,以允许用户根据品牌,产品类别和价格范围优化相同的搜索(订单无关紧要)。
以下实施可能无法正常工作,因为我只提供了一种说明问题的方法。
表格: 产品
| id | name | price_id |
| 1 | test1 | 1 |
| 2 | test2 | 2 |
| 3 | test3 | 1 |
prices_ranges
| id | price_from | price_to |
| 1 | 100 | 200 |
| 1 | 200 | 300 |
product_attributes
| product_id | id_attribute |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 5 |
| 2 | 5 |
| 2 | 3 |
| 3 | 5 |
属性
| id | name |
| 1 | attr1 |
| 2 | attr2 |
| 3 | attr3 |
| 4 | attr4 |
| 5 | attr5 |
| 6 | attr6 |
| 7 | attr7 |
我通过$ _GET传递url中的参数。我的多重过滤搜索代码看起来有点像这样:
//this generates the base query
$dynamicsql = get_by_filters();
//does the group by to discard duplicated products
$filtersql= $dynamicsql."group by p.id";
$query = mysql_query($filtersql) or die(mysql_error());
//the data is now ready to be presented but
$total = mysql_num_rows($query);
for($i=0; $i<$total; $i++):
$prods[$i] = mysql_fetch_object($query);
endfor;
//first let's build the html code for filtering the results
list_prices_filter($dynamicsql);
//i'll only show the code for the bellow function. the above is pretty similar
list_attr_filter($dynamicsql);
//now i print the results
foreach($prods as $prod):
//for the porpuse of this question will only print the product name
echo $prod['pname'];
endforeach;
返回已过滤的基本查询的函数
function get_by_filters(){
$dynamicsql = "SELECT p.name as pname,p.price_from,p.price_to,a.name,a.id FROM `products` p ";
$dynamicsql .= "left JOIN `price_ranges` pr ON p.price_id = pr.id ";
$dynamicsql .= "LEFT JOIN `product_attributes` pa ON w.id = pa.product_id ";
$dynamicsql .= "LEFT JOIN `attributes` a ON a.uid = pa.id_attribute where 1=1 ";
foreach($_GET as $parameter=>$value):
switch($parameter):
case 'price':
$dynamicsql .= " AND p.price = ".$value;
break;
//filtro por zip code
case 'attr':
$dynamicsql .= " AND a.id = ".$value;
break;
default:
$dynamicsql .= "AND ".$parameter." = ".$value." ";
break;
endswitch;
endforeach;
return $dynamicsql;
}
列出属性选择的功能:
function get_special_care($dynamicsql){
$sql = "SELECT * FROM attributes a";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
$output = '<ul>';
for($i=0; $i<$total;$i++):
$result[$i] = mysql_fetch_object($query);
$active = ($_GET['attr'] == $result[$i]->uid) ? 'active':'';
$workerstotal = mysql_query($dynamicsql . " and a.id ={$result[$i]->uid} group by p.id");
$totalworkers = mysql_num_rows($workerstotal);
$output .= '
<li class="'.$active.'">
<a href="'.url_increment('attr='.$result[$i]->uid).'">'.$result[$i]->name.'<span class="totalfound">('.$totalworkers.')</span></a>
<a title="Remove" href="'.url_decrement('attr').'" class="remove-criteria"><img src="'image_src_url" alt="Remove" /></a>
</li>';
endfor;
$output .= '</ul>';
echo $output;
}
函数url_increment()和url_decrement只是将该过滤器添加或删除到url。我没有列出它,因为我认为它与问题无关。
好介绍!!我的问题是,如果用户在第一步中选择属性5,代码会显示产品1,2和3,但我无法再次筛选属性!
在这种情况下,函数get_special_care($ dynamicsql)表示属性1,2有0个产品(产品1应该有1个)和3个(产品1和2应该是两个匹配)。
我几乎可以肯定这是因为在:
中生成基本查询的函数case 'attr':
$dynamicsql .= " AND a.id = ".$value;
break;
default:
但我无法弄清楚如何解决这个问题。
此代码还有另一个问题,因为一旦您第一次选择该属性,网址就像www.teste.com/search&attr=5。在第二次点击后(假设用户点击属性1),网址将是www.teste.com/search&attr=5&attr=1这也是一个问题(我认为)
有人可以帮忙吗?我相信你们中的许多人已经实施了类似的算法,如果你能提供帮助,那将非常棒。
最后对于长篇文章感到抱歉,但我想解释一下自己,也许它可以作为未来帮助的参考......
提前致谢!!
答案 0 :(得分:1)
我到目前为止唯一可以发现的错误是
case 'price': $dynamicsql .= " AND p.price = ".$value;
p.price请参阅products.price,您的原产品表中没有价格字段。
有任何查询例外吗?
答案 1 :(得分:1)
我设法解决了这个问题如下:
创建了一个从url获取所有属性的函数(普通$ _GET ['name'] ony返回属性= 5,如www.test.com/?mod=search&attribute=2&attribute=5
function getFilter(){
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param )
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
return $params;
}
然后我改变了函数get_by_filters
function get_by_filters(){
$dynamicsql = "SELECT p.name as pname,p.price_from,p.price_to,a.name,a.id FROM `products` p ";
$dynamicsql .= "left JOIN `price_ranges` pr ON p.price_id = pr.id ";
$dynamicsql .= "LEFT JOIN `product_attributes` pa ON w.id = pa.product_id ";
$dynamicsql .= "LEFT JOIN `attributes` a ON a.uid = pa.id_attribute where 1=1 ";
foreach($_GET as $parameter=>$value):
switch($parameter):
case 'price':
$dynamicsql .= " AND p.price = ".$value;
break;
//changes here
case 'attr':
$get = tools::getFilter();
foreach ($get['attr'] as $attrs => $value) {
$filter .= " and w.uid in (select a from `product_attributes` where attribute_id= {$value}) ";
}
default:
$dynamicsql .= "AND ".$parameter." = ".$value." ";
break;
endswitch;
endforeach;
return $dynamicsql;
}
最后我们必须提供所有活动的属性过滤器,所以我更改了这个函数,如下所示:
function get_special_care($dynamicsql){
$sql = "SELECT * FROM attributes a";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
//changes here
$get = tools::getFilter();
$output = '<ul>';
for($i=0; $i<$total;$i++):
$result[$i] = mysql_fetch_object($query);
//changes here
foreach ($get['attr'] as $attr => $value) {
$active = ($value == $result[$i]->uid) ? 'active':$active;
}
$workerstotal = mysql_query($dynamicsql . " and a.id ={$result[$i]->uid} group by p.id");
$totalworkers = mysql_num_rows($workerstotal);
$output .= '
<li class="'.$active.'">
<a href="'.url_increment('attr='.$result[$i]->uid).'">'.$result[$i]->name.'<span class="totalfound">('.$totalworkers.')</span></a>
<a title="Remove" href="'.url_decrement('attr').'" class="remove-criteria"><img src="'image_src_url" alt="Remove" /></a>
</li>';
endfor;
$output .= '</ul>';
echo $output;
}