I have a Database with a category table using the Nested Approach in MySql. But I keep hitting a wall. The Site contains “sections” species (As this is a pet store). I already have a query that returns all the categories that have products in them for each species.
Now my problem is that I want to be able to only show categories that have products under them. So for example if I create the category food with a child adult. Then the system should not list the category under any species. But as soon as I create a Product under the adult category the system should list Food with the child Adult .
The Table is currently structured as follows:
Category:
- id
- top
- name
- lft
- rgt
- created (for the sake of completeness)
- lastupdated (for the sake of completeness)
So I am able to build a full tree from the table for each species but I need the categories to only be visible once they have products in them for the species.
Sounds simple enough but for some reason I can’t get my brain around this. Any Help? What would be the best way to approach this?
Update:
The query I’m using to get the Categories:
SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND
foodcategories.food=foods.id AND foodcategories.category=node.id
AND brands.id=foods.brand AND foods.species={speciesid}
I then execute a query to get all the categories and their lft,rgt. Which I can built the tree from.
After a couple of days struggling a finally thought of a solution and implemented it.
I can execute the following query and get the entire path to and from a category,
then I thought if I could this why not include this path in the category query. So when I retrieve the categories that have products in them I can simply parse this string and match the id to a list of categories that I also retrieve.
This is quite a bit of looping. So I cache the menu and only generate it once every 5 min.
So that’s it! 3 Queries!
products in them.
with a certain ID.
On to the next challenge !