How do I make a filtering engine for my online shop?
My situation: I have tables
/* Stored products variants */
products_variants (id_product_variant,title,...)
/* Stored parameters with its values */
parameters(id_parameter, units, title,...)
/* Stored connection between variant and parameter value */
products_variants_parameters (parameter_id, product_id)
I need to do this:
The user selects value1
,value2
of parameter1
and value2
of parameter2
for example, so I need to select variants which has:
value1
or value2
of parameter1
AND value2
of parameter2
.
I tried to do something like this:
products_variants_parameters.parameter_id IN (value1, value2) AND
products_variants_parameters.parameter_id IN (value2)
But it returns an empty result. How do I update it to make it working?
EXAMPLE DATA:
products_variants [id_product_variant, title]
{
[1,Product 1],
[2,Product 2],
[3,Product 3]
}
parameters [id_parameter, title]
{
[1,Color: red],
[2,Color: blue],
[3,Size: XXL],
[4,Size: M]
}
products_parameters [product_id, parameter_id]
{
[1,1],
[1,4],
[2,1],
[2,3],
[3,2],
[3,4]
}
Customer checked:
Color: red
Color: blue
Size: M
==> So I want to return all products with color Red or Blue and in size M. For this example data products:
1 - Product 1 (Red, M)
3 - Product 3 (Blue, M)
SOLVED: Thanks this thread: How to filter SQL results in a has-many-through relation