romio Posted March 28, 2006 Share Posted March 28, 2006 Am currently working on php Product Catalogue system, DB is created and is running fine, small problem with the menu though, each category might have many sub_categories, and here were the problem starts, if i have cat_1 with sub cat_1_1 and cat_1_2 and cat_2 with sub cat_2_1 then codes works fine, i need to have a general function that would work on all depth, i cant just keep creating a new sql or a function code for every sub_categories! so i though of a recursive function or an array.. Any help would be appreciated.[code]function find_root_categories() { $catID = $_GET['cat']; $find_parent_categories = "SELECT node.category_id ,node.name, (COUNT(parent.name) - 1) AS depth FROM categories AS node, categories AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name HAVING depth=1 ORDER BY node.lft"; $result_parent_categories = mysql_query($find_parent_categories); $num_rows_parent_categories = mysql_num_rows($result_parent_categories); if(!($num_rows_parent_categories)) { echo"<tr> <td valign=top align=center>No Records To Display</td> </tr>"; } else { while($row = mysql_fetch_array($result_parent_categories)) { $name= $row['name']; $catid = $row['category_id']; echo" <a href='?cat=$catid'>$name</a><br /> "; if($catID == $catid){ get_sub_cat($catID); } } } }[/code][code]function get_sub_cat($catID){ $sql = "select * from categories where parent = $catID"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // now, retrieve all descendants of the $root node $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $name= $row['name']; $catid = $row['category_id']; echo" <a href='?cat=$catid'>$name</a><br /> "; if($catID == $catid){ get_sub_cat($catid); } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5990-php-help-with-menu-bar/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.