简体   繁体   English

动态多级下拉菜单 php sql

[英]dynamic multi level dropdown menu php sql

I have nav menu in html and i want to create a loop everything is fine but the item that have children echo twice: i know where is the problem but i can't figure out how to solve it :( my sql table named menu我在 html 中有导航菜单,我想创建一个循环,一切都很好,但是有孩子的项目回声两次:我知道问题出在哪里,但我不知道如何解决它:(我的 sql 表名为菜单

and this is my php:这是我的 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>' ;
    }
}

?>

and this the result: enter image description here这就是结果:在此处输入图像描述

i know it happen because the father item that hold the sub menu's called in $get once and another time when it need other css class.i tried if, foreach, while and many things.我知道它的发生是因为保存子菜单的父项在 $get 中调用一次又一次,当它需要其他 css class.i 尝试过 if、foreach、while 和许多事情时。 i need that item that holds submenus should have "menu-item-has-children" class otherwise its not show the sub menus.我需要包含子菜单的项目应该有“menu-item-has-children” class 否则它不会显示子菜单。

Here is see that Our Products from the database is printed twice.This is because you are fetching and echoing $row2['name'] twice but you can handle the first coming array by changing your SQL query.这里可以看到数据库中的Our Products打印了两次。这是因为您获取并回显$row2['name']两次,但是您可以通过更改 SQL 查询来处理第一个到来的数组。

Change your first SQL query to将您的第一个 SQL 查询更改为

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

Querying this to database you will get all the NULL values in the array excluding OUR PRODUCTS and thus it will be echoed only once by the second query.将其查询到数据库,您将获得数组中不包括OUR PRODUCTS的产品的所有 NULL 值,因此第二个查询只会回显一次。

When a menu item, have childrens then echo menu with menu-item-has-children class, otherwise echo a simple menu and move on.当一个菜单项时,有子项然后用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>' ;
    }
}

?>

I tried the above to answer all is working only a few level dropdown menu.我尝试以上方法来回答所有问题,只有几级下拉菜单。

If you want to really create your menu for multi-level dropdown as like tree structure just use the bellow function.如果您想真正为多级下拉菜单创建树状结构,只需使用下面的 function。 for more detailing once go through更详细的一次 go 通过

https://bootstrapfriendly.com/blog/dynamic-multi-level-dropdown-sticky-menu-in-php-mysql-using-bootstrap/ 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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