开发者

Dynamic drop down menu from Mysql Result set

开发者 https://www.devze.com 2023-03-20 17:51 出处:网络
I would like to create a dynamic menu from following result set. Html structure should as follows, <ul class=\"menu\">

I would like to create a dynamic menu from following result set.

Dynamic drop down menu from Mysql Result set

Html structure should as follows,

<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>

Tried as following,

<?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>':'';        ?>开发者_StackOverflow社区;

                        <?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; ?>

It fails when close closing.

Please help.

Thanks


I have rearranged your PHP code a bit, please try the following:

<?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; ?>


you can use Accordion menu for this task.

See this link http://chandreshmaheshwari.wordpress.com/2011/05/27/accordian-menu/

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消