动态多级下拉菜单 php sql

dynamic multi level dropdown menu php sql

我在 html 中有导航菜单,我想创建一个循环,一切都很好,但是有子项的项目回显了两次!我知道问题出在哪里,但我不知道如何解决它:( my sql table named menu

这是我的 php:

      $db = mysqli_connect('localhost', 'root', 'password', 'aftab');
<?php
$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
    echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
    $id = $rowmenu['id'] ;
    $check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
    if ( mysqli_num_rows($check) ) { 
         echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ; 
         echo '<ul class="sub-menu">' ;
        while ( $row2 = mysqli_fetch_assoc($check) ) {
            echo '<li class="menu-item-302"><a href="' . $row2['link'] . '">' . $row2['name'] . '</a></li>' ;
        }
        echo '</ul>' ;
    } else {
        echo '</li>' ;
    }
}

?>

这是结果:enter image description here

我知道它会发生,因为包含子菜单的父项在需要其他 css class.i 时调用 $get 一次又一次尝试了 if 、 foreach 、 while 和许多东西。 我需要包含子菜单的项目应该有 "menu-item-has-children" class 否则它不会显示子菜单。

这里看到打印了数据库中的 Our Products twice.This 是因为你正在获取和回显 $row2['name'] 两次,但你可以处理第一次通过更改您的 SQL 查询来获得数组。

将您的第一个 SQL 查询更改为

$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL AND name != 'OUR PRODUCTS'");

将此查询到数据库,您将获得数组中除 OUR PRODUCTS 之外的所有 NULL 值,因此第二次查询只会回显一次。

当一个菜单项时,有孩子然后回显菜单 menu-item-has-children class,否则回显一个简单的菜单并继续。

<?php

$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
    $id = $rowmenu['id'] ;
    $check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
    $haveSubMenu = mysqli_num_rows($check);

    if($haveSubMenu)
        echo '<li class="menu-item menu-item-type-post_type menu-item-object-page  menu-item-656 menu-item-has-children"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
    else
        echo '<li class="menu-item menu-item-type-post_type menu-item-object-page  menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;

    if ($haveSubMenu) 
    { 
        echo '<ul class="sub-menu">' ;
        while ( $row2 = mysqli_fetch_assoc($check) ) {
           echo '<li class="menu-item-302"><a href="' . $row2['link'] . '">' . $row2['name'] . '</a></li>' ;
        }
        echo '</ul>' ;
    } else {
        echo '</li>' ;
    }
}

?>

我尝试了上面的回答,但只有几级下拉菜单可以正常工作。

如果你真的想为 multi-level 下拉菜单创建像树结构一样的菜单,只需使用波纹管功能。通过

了解更多详情

https://bootstrapfriendly.com/blog/dynamic-multi-level-dropdown-sticky-menu-in-php-mysql-using-bootstrap/

<?php
include_once("connection.php");
$query = "SELECT id, label, link, parent FROM menus ORDER BY  sort ASC, label";
$result = mysqli_query($conn, $query) or die("database error:" . mysqli_error($conn));
// Create an array to conatin a list of items and parents
$menus = array(
    'items' => array(),
    'parents' => array()
);
// Builds the array lists with data from the SQL result
while ($items = mysqli_fetch_assoc($result)) {
    // Create current menus item id into array
    $menus['items'][$items['id']] = $items;
    // Creates list of all items with children
    $menus['parents'][$items['parent']][] = $items['id'];

    //echo  $items;
}

// function to create dynamic treeview menus
function createMenu($parent, $menu)
{
    $html = "";
    if (isset($menu['parents'][$parent])) {
        // $html .= '<ul class="sina-menu sina-menu-right" data-in="fadeInLeft" data-out="fadeInOut">';
        foreach ($menu['parents'][$parent] as $itemId) {
            if (!isset($menu['parents'][$itemId])) {
                $html .= "<li > 
                         <a  href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] . "</a> 
                     </li>";
            }
            if (isset($menu['parents'][$itemId])) {
                $html .= "<li class='dropdown'> 
                  <a class='dropdown-toggle' data-toggle='dropdown' href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] .  "</a>";
                $html .= '<ul class="dropdown-menu">';
                $html .= createMenu($itemId, $menu);
                $html .= '</ul>';

                $html .= "</li>";
            }
        }
        // $html .= "</ul>";
    }
    return $html;
}