I want to create my dropdown menu from a mysql query, but i'm having trouble with the sub-items.
My basic table:
NavigationID ParentID Name Url
1 1 Home home
2 2 About about
3 3 Products products
4 3 Category1 #
5 3 Category2 #
6 4 Product1 #
7 5 Product2 #
My simple MySQL Query and adding to array:
class Navigation{
private $data;
public function __construct($par){
if(is_array($par))
$this->data = $par;
}
public function __toString(){
return '<li><a href="'.$this->data['Url'].'">'.$this->data['Name'].'</a></li>';
}
}
$query = mysql_query("SELECT * FROM Navigation n") or die(mysql_error());
$num = mysql_num_rows($query);
$menuitems = array();
while($row = mysql_fetch_assoc($query)){
$menuitems[] = new Navigation($row);
}
echo '<div id="nav"><ul>';
foreach($menuitems as $item){
echo $item;
}
echo '</ul></div>';
The result of this is:
<div id="nav"><ul>
<li><a href="home">Home</a></li>
<li><a href="about">About</a></li>
<li><a href="products">Products</a></li>
<li><a href="#">Category1</a></li>
<li><a href="#">Category2</a></li>
<li><a href="#">Product1</a></li>
<li><a href="#">Product2</a></li>
</ul></div>
But what I would REALLY like is this:
<div id="nav"><ul>
<li><a href="home">Home</a></li>
<li><a href="about">About</a></li>
<li><a href="products">Products</a>
<ul>
<li><a href="#">Category1</a>
<ul>
<li><a href="#">Product1</a></li>
</ul>
</li>
<li><a href="#">Category2</a>
<ul>
<li><a href="#">Product2</a></li>
</ul>
</li>
</ul>
</li>
</ul></div>
How can I achieve this result? I've tried many other examples, but none seems to help me. Maybe I'm not searching for the right thing.