I have a code that generates dynamic menu, I cannot seem to make the list be similar to the static one on html file, like for example the pathologist children is generated in two different ways instead of being generated in the same unordered list under same class
I have the following array from mysql table
Array ( [0] => stdClass Object ( [id] => 1 [name] => Obituaries [parent] => 0 ) [1] => stdClass Object ( [id] => 2 [name] => Caskets [parent] => 0 ) [2] => stdClass Object ( [id] => 3 [name] => Pathologist [parent] => 0 ) [3] => stdClass Object ( [id] => 4 [name] => Hospital [parent] => 0 ) [4] => stdClass Object ( [id] => 5 [name] => Flowers [parent] => 0 ) [5] => stdClass Object ( [id] => 6 [name] => Hearse [parent] => 0 ) [6] => stdClass Object ( [id] => 7 [name] => Small ones [parent] => 2 ) [7] => stdClass Object ( [id] => 8 [name] => Big ones [parent] => 2 ) [8] => stdClass Object ( [id] => 9 [name] => Red Rose [parent] => 0 ) [9] => stdClass Object ( [id] => 12 [name] => Pathologist 1 [parent] => 3 ) [10] => stdClass Object ( [id] => 13 [name] => Pathologist 2 [parent] => 3 ) )
I want to generate a dynamic menu which looks exactly like this, see my html (static) example
<ul>
<li><a href="#">Obituaries<i class="icons icon-right-dir"></i></a></li>
<li><a href="#">Caskets <i class="icons icon-right-dir"></i></a>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Smlal Ones</a></li>
<li><a href="#">Big Ones</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Pathologists <i class="icons icon-right-dir"></i></a>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Pathologist 1</a></li>
<li><a href="#">Pathologist 2</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Hospitals<i class="icons icon-right-dir"></i></a></li>
</ul>
My Code(PHP)
function menuBuilder() {
$tree = $this->_API->getData('categories');
$node_id = '';
echo '<ul>';
foreach ($tree as $node):
if ($node->parent == 0):
echo '<li><a href="#">'.
$node->name .'<i class="icons icon-right-dir"></i></a> ';
$node_id = $node->id;
$this->subMenuBuilder($tree, $node_id);
echo '</li>';
endif;
endforeach;
echo '</ul>';
}
function subMenuBuilder($tree, $node_id) {
foreach ($tree as $branch):
if ($branch->parent == $node_id):
echo '<ul class="sidebar-dropdown"><li><ul>';
echo '<li><a href="#">'.
$branch->name .'</a>';
$this->subMenuBuilder($tree, $branch->id);
echo '</ul><li></li>';
echo '</ul>';
endif;
endforeach;
}
What php generates
<ul>
<li><a href="#">Obituaries<i class="icons icon-right-dir"></i></a> </li>
<li>
<a href="#">Caskets<i class="icons icon-right-dir"></i></a>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Small ones</a>
</ul>
</li>
</li>
</ul>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Big ones</a>
</ul>
</li>
</li>
</ul>
</li>
<li>
<a href="#">Pathologist<i class="icons icon-right-dir"></i></a>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Pathologist 1</a> //difference here
</ul>
</li>
</li>
</ul>
<ul class="sidebar-dropdown">
<li>
<ul>
<li><a href="#">Pathologist 2</a> //difference here
</ul>
</li>
</li>
</ul>
</li>
<li><a href="#">Hospital<i class="icons icon-right-dir"></i></a> </li>
<li><a href="#">Flowers<i class="icons icon-right-dir"></i></a> </li>
</ul>
How can I make the generated tags be exactly same as the static one above, especially i know the problem could lie in the subMenuBuilder function. Any Suggestions?