我有一个生成动态菜单的代码,我似乎无法使列表与html文件中的静态列表相似,例如病理学家子项以两种不同的方式生成,而不是在同一个无序列表中生成在同一个班级
我有来自mysql表的以下数组
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 ) )
我想生成一个看起来与此完全相同的动态菜单,请参阅我的html(静态)示例
<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>
我的代码(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;
}
php生成什么
<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>
如何使生成的标签与上面的静态标签完全相同,尤其是我知道问题可能在于subMenuBuilder函数。有什么建议吗?