Reputation: 762
Im trying to biuld a filter algorithm for my product list.
For example I have 2 Televisions.
First one is: screen Size 117 Cm, brand Samsung. Second one is: screen size 203 CM, brand Sharp.
And i have the following filters:
Brand: Samsung, Sharp
Screen size: 117 Cm, 203 CM
If I want to see only Samsung TVs and I check the Samsung Filter, how can I reach to conclusion that I should remove Screen size 203 from filters, because samsung have only 117 cm tv.
But if i check sharp tv and samsung in the same time, then I should see both screen size filters.
I m asking for an idea for some php/mysql algorithm.
I hope you understand what i m asking.
For an example look here: http://www.ebay.co.uk/sch/Televisions-/11071/i.html?_dcat=11071&Display%2520Technology=LED%2520LCD&rt=nc
The website knows how many products it has have before user checks any filter: eg: "40" - 49" (326)) -> they know that they have 326 products before user checks that filter.
In short : The checkboxes you check in the filters, should be disabled if the combination is not available for the products.
I have 3 tables: products(id_product,name), filters(id_filter,name,id_category), filters_values(id,id_product,id_filter,value)
In my product_list page it should appear only filter that once being checked they return products.
My first thought is to make sql from each filter value combination. Eg 10 filters with 4 values: 40 sql, just to check if filters return products, But this is way to much stress on db
For example if I have one filter with 4 values, i should make 4 query in order to see if I have products on specific value.
I i have 10 filter each having 4 values i should do 40 sql query. Which is a lot. How can i do this faster?
Thanks
Upvotes: 2
Views: 3713
Reputation: 101
I usually do something like this in php:
// queries all products if no filter are specified
$sql_query = "SELECT * FROM products ";
// add fileters e.g. from $_GET array
$i = 0;
foreach($filter_array as $filter -> $filter_value)
{
if( ! $i)
{
$sql_query .= "WHERE ";
}
else
{
$sql_query .= " AND ";
}
$sql_query .= $fliter . " = '" . $filter_value . "'";
$i++;
}
// generating something like:
// "SELECT * FROM products WHERE color = 'green' AND brand = 'samsung'"
// now execture $sql_query
This is just a sketch but I think you get the idea
Upvotes: 4