我想从以下结果集创建一个动态菜单。
Html结构应如下,
<ul class="menu">
<li>Menu 1</li>
<ul class='submenu'>
<li>Submenu1</li>
<li>Submenu2</li>
</ul>
<li>Menu2</li>
<ul class='submenu'>
<li>Submenu1</li>
<li>Submenu2</li>
</ul>
<li>Menu 3</li>
</ul>
尝试如下,
<?php $cat = 0;?>
<?php foreach($this->submenus as $submenu): ?>
<?php if($cat!= $submenu->category_id): ?>
<li><?php echo $submenu->category_name ?></li>
<?php echo (!empty($submenu->subcategory_name))?'<ul>':''; ?>
<?php $flag = $submenu->category_id; ?>
<?php endif;?>
<li class='sub'><?php echo $submenu->subcategory_name ?></li>
<?php if($cat!= $flag && $cat > 0): ?>
</ul>
<?php endif;?>
<?php $cat = $submenu->category_id;?>
<?php endforeach; ?>
关闭时失败。
请帮忙。
由于
答案 0 :(得分:0)
您可以使用Accordion菜单执行此任务。
请参阅此链接http://chandreshmaheshwari.wordpress.com/2011/05/27/accordian-menu/
感谢。
答案 1 :(得分:0)
我稍微重新安排了您的PHP代码,请尝试以下操作:
<?php $cat = 0;?>
<?php $submenu_flag = 0;?>
<?php foreach($this->submenus as $submenu): ?>
<?php if($cat!= $submenu->category_id): ?>
<?php if($submenu_flag > 0): ?>
</ul>
<?php $submenu_flag = 0;?>
<?php endif;?>
<li><?php echo $submenu->category_name ?></li>
<?php if(!empty($submenu->subcategory_name)): ?>
<ul>
<?php $submenu_flag = 1; ?>
<?php endif;?>
<?php endif;?>
<?php echo (!empty($submenu->subcategory_name))?'<li class='sub'><?php echo $submenu->subcategory_name ?></li>':''; ?>
<?php $cat = $submenu->category_id;?>
<?php endforeach; ?>