PHP:多级菜单或嵌套菜单

时间:2016-10-03 04:48:38

标签: php

我有嵌套菜单或多级菜单的代码。但我没有得到我想要的结果。我对PHP编程语言有非常基本的了解,所以请耐心等待。

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

这就是我做的 enter image description here

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

使用此代码管理以获得我想要的结果

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