The category table looks like somewhat as below:
id -- name -- parent_id
1 -- Men -- 0
2 -- Women -- 0
3 -- Shirts -- 1
4 -- Half-sleeve -- 3
5 -- Full-sleeve -- 3
Relationship table:
Product_id -- Category Id
1 -- 2
2 -- 2
3 -- 4 ....
I can retrieve the number of products in any one category and its immediate sub categories with ease with ease. But if there are more than 2 levels things get messy.
So my question is How do I get the number of all of the products in Men and its sub categories. Or Shirts and its subcategories?
Any ideas, Thanks.
UPDATE:
I know there is Nested Set Model but I am not in position to change the structure to that now.
If it is possible I would check out Managing Hierarchical Data in MySQL.
It is hard to get your head around at first, but it makes tasks like this much easier.
If you can’t do this, you will have to do a recursive function, e.g.: