Hi,
products in my database are listed in multiple categories..
i want to be able to return the images that are listed in all the categories selected, its easy to list all the products that are in either category ... but i want them to only display if listed in both/all the categories search on.
when searching i POST
$_POST['c']
as an array of category IDs from a form
I have tables as follows
products
categories
products_to_categories
products_to_categories can contain multiple rows for each product...
So i need to select
DISTINCT product WHERE the product is listed in all the submitted categories...
iv been trying to do...
"SELECT DISTINCT(product) FROM products_to_categories WHERE category = '1' AND category = '2'"
but that returns nothing, even though there is a product listen in both categories 1 + 2
This can be done with the query surely? but i cant figure it out 🙁
here is my current code
it builds a list of the product ids i need to output to the results page
$first = TRUE;
foreach($_POST['c'] as $this_cat)
{
if($first == TRUE)
{
$catsql = "category = '".$this_cat."'";
$first = FALSE;
}
else
{
$catsql .= " OR category = '".$this_cat."'";
}
}
$numcats = count($_POST['c']);
$query1 = "SELECT * ".
"FROM ".PRODUCT_CATEGORIES_TABLE." ".
"WHERE ".$catsql." ORDER BY product";
$result1 = mysql_query($query1)
or die ("Tried to execute1: <i>".$query1."</i><br/>
MYSQL said: <i>".mysql_error()."</i><br/>//end");
$sameasbefore = NULL;
$inallcats = TRUE;
while($row1 = mysql_fetch_array($result1))
{
if($sameasbefore != $row1['product'])
{
$cats[] = $row1['product'];
$sameasbefore = $row1['product'];
}
}
I appreciate your help
Rob