我尝试使用 codeigniter 创建多级菜单,这是我用来回显菜单的两个函数。
function loop_array($array = array(), $parent_id = 0) {
if (!empty($array[$parent_id])) {
// echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li><a class = "parent"><span>';
echo $items['label'];
echo '</span></a>';
loop_array($array, $items['id']);
echo '</li>';
}
echo '</ul>';
}
}
function display_menus($menu_items) {
// var_dump($menu_items);
$array = array();
foreach ($menu_items as $rows) {
$array[$rows['parent']][] = $rows;
}
loop_array($array);
}
然后输出的html代码就像
<div id='Mymenu' style=' color: #000; width: 960px; background-image:url(); clear: both; text-align: center; height: 46px;'>
<div id="menu">
<ul class="menu">
<li><a class = "parent"><span>Home</span></a></li><li><a class = "parent"><span>Code</span></a><li><a class = "parent"><span>PHP</span></a><li><a class = "parent"><span>Scripts</span></a><li><a class = "parent"><span>Archive</span></a><li><a class = "parent"><span>Snippet</span></a></li></ul></li></ul></li><li><a class = "parent"><span>Help</span></a></li></ul></li><li><a class = "parent"><span>CSS</span></a></li></ul></li><li><a class = "parent"><span>Contact</span></a></li></ul> </ul>
</div>
</div>
但我需要这样做
<div id='Mymenu' style=' color: #000; width: 960px; background-image:url(); clear: both; text-align: center; height: 46px;'>
<div id="menu">
<ul class="menu">
<li><a href="index" class="parent"><span>HOME</span></a>
<div><ul>
<li><a href=""><span>Vertical Menu</span></a>
<li><a href=""><span>Slide show</span></a>
</li>
<li><a href="#" class="parent"><span>ABOUT US</span></a></li>
<li><a href="news"><span>NEWS</span></a></li>
<li class="last"><a href="#"><span>ACADEMICS</span></a></li>
<li class="last"><a href="#"><span>PROGRAMME</span></a></li>
<li class="last"><a href="#"><span>ADMISSIONS</span></a></li>
<li class="last"><a href="courses"><span>COURSES</span></a></li>
<li class="last"><a href="contact_us"><span>CONTACT US</span></a></li>
<li class="last"><a href="feedback"><span>Feedback</span></a></li>
</ul>
</div>
谁能给我一个方法来做到这一点?