jerastraub Posted February 2, 2007 Share Posted February 2, 2007 I am trying to make a Dynamic Tree Menu out of my database. I have products feeds that have both a category and subcatrgory. I want to create the tree menu dynamically from the database. I know how to create a nav bar with just cateogies, but I am having trouble coming up with the code for have it cycle the Categories and if the Category contains Subcategories with will display those and so on. Can anyone offer me any tips on this! Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/ Share on other sites More sharing options...
Cagecrawler Posted February 2, 2007 Share Posted February 2, 2007 I'd do something like this: <?php $query=mysql_query("SELECT * FROM menu WHERE type='mainmenu'") while($main=mysql_fetch_array($query)) { $name=$main['name']; echo "<li>".$name."<ul>"; $query2=mysql_query("SELECT * FROM menu WHERE type='submenu' AND section='$name'"); while($sub=mysql_fetch_array($query2)) { echo "<li><a href=\"linktopage.php\">".$sub['name']."</a></li>"; } echo "</ul></li>"; } ?> Hopefully you can adjust it to fit your database. It's untested, so post if you have any problems. Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175173 Share on other sites More sharing options...
jerastraub Posted February 2, 2007 Author Share Posted February 2, 2007 Okay, I am having a problem while testing the code. I have changed it to fit my database as follows: <?php $sql_query = "select DISTINCT Category from Products"; $result = mysql_query($sql_query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { $name=$row['name']; echo "<li>".$name."<ul>"; $query2=mysql_query("select DISTINCT Subcategory from Products ORDER by Subcategory"); while($sub=mysql_fetch_array($query2)) { echo "<li><a href=\"linktopage.php\">".$sub['name']."</a></li>"; } echo "</ul></li>"; } ?> but now i am getting the follow error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Category ORDER by Category' at line 1 I don't see what is causing the error When I used your code with my info, <?php $query=mysql_query("select DISTINCT Category from Products") while($main=mysql_fetch_array($query)) { $name=$main['name']; echo "<li>".$name."<ul>"; $query2=mysql_query("select DISTINCT Subcategory from Products ORDER by Subcategory"); while($sub=mysql_fetch_array($query2)) { echo "<li><a href=\"linktopage.php\">".$sub['name']."</a></li>"; } echo "</ul></li>"; } ?> i got this error Parse error: syntax error, unexpected T_WHILE in /home/w21510/public_html/lampworld/nav.php on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175592 Share on other sites More sharing options...
jerastraub Posted February 3, 2007 Author Share Posted February 3, 2007 Okay, Finally got it to work: <?php $sql_query = "select DISTINCT Custom1 FROM lampworld"; $result = mysql_query($sql_query) or die (mysql_error()); while ($row = mysql_fetch_array($result)) { $name=$row['Custom1']; echo "<li>".$name."<ul>"; $query2=mysql_query("select DISTINCT Custom2 from lampworld ORDER by Custom1"); while($sub=mysql_fetch_array($query2)) { echo "<li><a href=\"linktopage.php\">".$sub['name']."</a></li>"; } echo "</ul></li>"; } ?> But now when I load the page i only get: <li>Home/Family<ul><li><a href="linktopage.php"></a></li></ul></li> Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175829 Share on other sites More sharing options...
jerastraub Posted February 3, 2007 Author Share Posted February 3, 2007 * Bump * Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175987 Share on other sites More sharing options...
jerastraub Posted February 4, 2007 Author Share Posted February 4, 2007 * Bump * Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-176549 Share on other sites More sharing options...
jerastraub Posted February 4, 2007 Author Share Posted February 4, 2007 Is there anyone out there that has any ideas on how i can make this work! Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-176715 Share on other sites More sharing options...
sasa Posted February 4, 2007 Share Posted February 4, 2007 change line $query2=mysql_query("select DISTINCT Custom2 from lampworld ORDER by Custom1"); to $query2=mysql_query("select DISTINCT Custom2 AS name from lampworld WHERE Custom1='$name'"); Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-176772 Share on other sites More sharing options...
jerastraub Posted February 4, 2007 Author Share Posted February 4, 2007 Thank you so much Sasa! It works perfectly now! Quote Link to comment https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-177007 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.