mysql - PHP-SQL. Non-recursive way to select Categories and multiple Subcategories from DB -
i'm trying find way every subcategory parent category, in case, dont know how many subcategories have, so, solution found using recursion.
i used:
public function recursivesel(&$tree, $db, $parent = 0){ $query = $db->query("select id, catname categories parentcategory=".$parent); while($category = $db->fetch_array($query)){ $tree .= $category['catname']; if($category['id'] != $parent){ $this->treeselcategories($tree,$db,$category['id']); } } }
but think if 1000 users navegating through categories , have 500 categories, can mess.
my category table builted 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 result needed:
games --action ----new courses --cooking ----tutorial ------easy --diy
is there way without use recursion? sql maybe? can change db structure if needed.
thank you!
Comments
Post a Comment