简体   繁体   中英

PHP: multilevel menu or nested menu

I have a code for nested menu or multilevel menu. but I don't get the result that I wanted. I have a very basic knowledge about PHP programming language so please bear with me.

<?php 
include("mycon.php");
$query1 = mysql_query("SELECT * FROM menu");
$row1 = mysql_fetch_array($query1);
$a = $row1['parent'];
?>
<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container">
    <a class="navbar-brand" href="#">Title</a>
    <ul class="nav navbar-nav">

            <?php


                    $query2 = mysql_query("SELECT * from menu");
                    while ($fetcharray2 = mysql_fetch_array($query2)) {
                        $query5 = mysql_query("SELECT * from menu where menu.parent = '0'");
                        while ($fetcharray4 = mysql_fetch_array($query5)) {
                    ?>
                    <li class="dropdown">
                    <?php

                    ?>
                   <a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo $fetcharray4['label']?><b class="caret"></b></a>
                   <ul class="dropdown-menu">
                   <?php 
                   $query3 = mysql_query("SELECT * from menu where menu.id and menu.parent like '$a'");
                    while ($fetcharray3 = mysql_fetch_array($query2)) {
                   ?>
                      <li><a href="<?php echo $fetcharray3['link'];?>"><?php echo $fetcharray3['label'];?></a></li>
                      <?php }?>
                   </ul>
                </li>

            <?php }}
                ?>

    </ul>
</div>
</nav>

that is how I did it 在此处输入图片说明

    // Select all entries from the menu table
$result=mysql_query("SELECT id, label, link, parent FROM menu ORDER BY parent, sort, label");
// Create a multidimensional array to conatin a list of items and parents
$menu = array(
    'items' => array(),
    'parents' => array()
);
// Builds the array lists with data from the menu table



    // Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
   $html = "";
   if (isset($menu['parents'][$parent]))
   {
      $html .= "
      <ul>\n";
       foreach ($menu['parents'][$parent] as $itemId)
       {
          if(!isset($menu['parents'][$itemId]))
          {
             $html .= "<li>\n  <a href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
          }
          if(isset($menu['parents'][$itemId]))
          {
             $html .= "
             <li>\n  <a href='".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a> \n";
             $html .= buildMenu($itemId, $menu);
             $html .= "</li> \n";
          }
       }
       $html .= "</ul> \n";
   }
   return $html;
}
echo buildMenu(0, $menu);
while ($items = mysql_fetch_assoc($result))
{
    // Creates entry into items array with current menu item id ie. $menu['items'][1]
    $menu['items'][$items['id']] = $items;
    // Creates entry into parents array. Parents array contains a list of all items with children
    $menu['parents'][$items['parent']][] = $items['id'];
}

http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu

Managed to get my desired result with this code

<?php 
  include("mycon.php");

?>
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
<div class="container">
    <a class="navbar-brand" href="#">Title</a>
    <ul class="nav navbar-nav">
   <?php
    $query = mysql_query("SELECT * FROM menu where menu.parent = '0'");
    while ($row = mysql_fetch_array($query)) {
?>
                <li class="dropdown">
                   <a href="#" class="dropdown-toggle" data-toggle="dropdown"><?php echo $row['label'];?><b class="caret"></b></a>
                   <?php
                    $a = $row['id'];
                    ?>
                      <ul class="dropdown-menu">
                      <?php
                        $query2 = mysql_query("SELECT * FROM menu where menu.parent = '$a'");
                        while($row2 = mysql_fetch_array($query2)){
                      ?>
                      <li><a href="<?php echo $row2['link'];?>"><?php echo $row2['label'];?></a></li>
                      <?php
                        }
                      ?>
                   </ul>
                    <?php
                   ?>
                </li>
   <?php }?>      
    </ul>
</div>
</nav>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM