Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175173
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175592
Share on other sites

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/36726-dynamic-tree-menu/#findComment-175829
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.