What I'm trying to do should be simple, but it is giving me a headache. This is it, simplified from the actual scenario to just leave the part I am struggling with, for clarity: DB with 2 tables, products and categories from which I want to print a list of categories and their IDs, but only if there are products in the categories.
- categories.catID
- products.productCat (which corresponds to categories.catID)
- categories.catName
I have a query something like this:
$sql = "SELECT * from categories, products WHERE products.productCat=categories.catID";
Then I am printing so:
while ($newArray = mysql_fetch_array($result)){
$catname = $newArray['catName'];
print "<option value=http://www.domain.com/products.php?cat=$newArray[catID]>";
print "$catname";
print "</option>";
}
This suppresses empty categories fine, but it means that the category name is repeated in the list for each product in that category.
How do I either select only unique values of catName, or alternatively print only unique values of catName?
I am thinking it is something to do with array_intersect, but cannot figure out exactly how to use that in this scenario.
Actually I've a feeling I stuck in a warped logic and there is a different and better way of doing what I want that I'm not seeing.