我试图建立一个多层次的搜索,让用户基于品牌,产品和价格范围的类别(顺序无关)细化相同的搜索。基于多级精炼搜索结果过滤MYSQL + PHP
下面的实现可能会无法正常工作是我只提供一种方式来说明我的问题。
表: 产品
| 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(应该有一个用于产品1)和3(应该是产品1和2的两个匹配)有0个产品。
我几乎可以肯定,这是因为其生成的基本查询功能:
case 'attr':
$dynamicsql .= " AND a.id = ".$value;
break;
default:
但我无法弄清楚如何解决这个问题。
此代码还有另一个问题,因为一旦您选择第一次url的属性看起来像www.teste.com/search & attr = 5。单击第二个时间后(假设用户点击属性1)将URL www.teste.com/search & ATTR = 5 & ATTR = 1这是一个问题也(我认为)
谁能帮助?我相信你们中的许多人已经实施了类似的算法,如果你能提供帮助的话会非常棒。
对长期职位最后遗憾,但我想解释一下,也许它作为今后帮助参考...
在此先感谢!
嗨,试试回声所有的查询运行时,以帮助您了解什么是真正发生的功能。它对复杂的sql生成算法有很大的帮助。例如,echo所有url属性和sql。所以输出就像url属性是“”,sql是“” – Will
你好!感谢您的回应。我已经做到了,我发现问题似乎在这里“$ dynamicsql。=”AND a.id =“。$ value;”因为我基本上告诉SQL只返回与该attribute_id相关的结果,但问题是,我不能改变SQL来匹配我想要的... – jribeiro
你介意查询当你运行get_special_care($ dynamicsql) – Will