I'm trying to find a way to get every subcategory from a parent category, in my case, I dont know how many subcategories I will have, so, the only solution I found is using recursion.
I used:
public function recursiveSel(&$tree, $db, $parent = 0){
$query = $db->query("SELECT id, catName FROM categories WHERE parentCategory=".$parent);
while($category = $db->fetch_array($query)){
$tree .= $category['catName'];
if($category['id'] != $parent){
$this->treeSelCategories($tree,$db,$category['id']);
}
}
}
But I think that if 1000 users are navegating through the categories and I have 500 categories, it can be a mess.
My category table is builted like this:
|||||||||||||||||||||||||||||
|| id || name || parent ||
|||||||||||||||||||||||||||||
|| 1 || games || 0 ||
|| 2 || courses || 0 ||
|| 3 || cooking || 2 ||
|| 4 || diy || 2 ||
|| 5 || action || 1 ||
|| 6 || new || 5 ||
|| 7 || tutorial|| 3 ||
|| 8 || easy || 7 ||
|||||||||||||||||||||||||||||
And the result needed:
games
--action
----new
courses
--cooking
----tutorial
------easy
--diy
Is there a way to do it without use recursion? Only with sql maybe? I can change the db structure if is needed.
Thank you!