searching different values for one field
Posted: Mon Jan 24, 2005 7:04 am
hi,
i have the following query to grap products that are in selected categories when posted though a form....
the results it giving are not what i want
heres the code...
This is returning the product ids for items in either of the selected categories....
what i want is for products ids that are in all the selected categories...
So if
product 1 is in cat 1 + 2
and product 2 is in just cat 1.
if i search for cat 1+2 only product 1 is returned...
in the database i have rows...
with the product ID and the category. so if a product is in multiple categories there is 1 or more rows with product id = 1 for example...
Hope someone has some ideas.
Thanks
Rob
i have the following query to grap products that are in selected categories when posted though a form....
the results it giving are not what i want
heres the code...
Code: Select all
foreach($_POSTї'c'] as $this_cat)
{
if($first == TRUE)
{
$catsql = "category = '".$this_cat."'";
$first = FALSE;
}
else
{
$catsql .= " OR category = '".$this_cat."'";
}
}
$query1 = "SELECT DISTINCT(product) FROM " . PRODUCT_CATEGORIES_TABLE . " " .
"WHERE " . $catsql;
$result1 = mysql_query($query1) or die ("Tried to execute1 :<i>" . $query1 . "</i><br/>
MYSQL said: <i>" . mysql_error() . "</i><br/>//end");
while($row1 = mysql_fetch_array($result1))
{
$catsї] = $row1ї'product'];
}what i want is for products ids that are in all the selected categories...
So if
product 1 is in cat 1 + 2
and product 2 is in just cat 1.
if i search for cat 1+2 only product 1 is returned...
in the database i have rows...
with the product ID and the category. so if a product is in multiple categories there is 1 or more rows with product id = 1 for example...
Hope someone has some ideas.
Thanks
Rob