带有 Php Mysql 的 Css 多级垂直菜单

Posted

技术标签:

【中文标题】带有 Php Mysql 的 Css 多级垂直菜单【英文标题】:Css multilevel vertical menu with Php Mysql 【发布时间】:2022-01-21 00:38:06 【问题描述】:

我有以下代码用于集成具有无限多级别的类别菜单

<ul id="menu_mb_ul" class="nt_mb_menu">
  <li class="menu-item menu-item-has-children only_icon_false">
    <a href="home-classic.html"><span class="nav_link_txt flex al_center">1 Category</span><span class="nav_link_icon ml__5"></span></a>
    <ul class="sub-menu">

      <li class="menu-item menu-item-has-children only_icon_false">
        <a href="home-header-01.html"><span class="nav_link_txt flex al_center">2 Category</span><span class="nav_link_icon ml__5"></span></a>
        <ul class="sub-sub-menu">
          <li class="menu-item">
            <a href="home-header-01.html">3 Category</a></li>

        </ul>
      </li>
    </ul>
  </li>
</ul>

我的 php 代码如下,但它没有显示第三级类别。我哪里错了?

function buildCategory2($parent, $category) 
$html = "";
if (isset($category['parent_cats'][$parent])) 
    if (!isset($category['parent_cats'][$cat_id])) 
        $html .= "<ul id='menu_mb_cat' class='nt_mb_menu'>\n";  
    
    else
    
        $html .= "<ul id='menu_mb_cat' class='nt_mb_menu'>\n";  
    
    foreach ($category['parent_cats'][$parent] as $cat_id) 
        if (!isset($category['parent_cats'][$cat_id])) 
            $html .= "<li class='menu-item'>\n  <a href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['category_name'] . "</a>\n</li> \n";
        
        else
        if (isset($category['parent_cats'][$cat_id])) 
            $html .= "<li class='menu-item menu-item-has-children only_icon_false'>\n  <a href='" . $category['categories'][$cat_id]['category_link'] . "'><span class='nav_link_txt flex al_center'>" . $category['categories'][$cat_id]['category_name'] . "</span><span class='nav_link_icon ml__5'></span></a> \n";
            
            $html .= "<ul class='sub-menu'><li class='menu-item'><span class='nav_link_txt flex al_center'>".buildCategory($cat_id, $category)."</span></li>";
            
            $html .= "</li>\n";
                $html .= "</ul>\n";
        
    
    $html .= "</ul> \n";

return $html;
echo buildCategory2(0, $category);

【问题讨论】:

你展示的没用,你需要展示你使用的是什么框架,比如bootstrap。您的基于 的菜单是通过 css+js 管理的,您也不显示它们。 【参考方案1】:

我自己找到了解决方案。

function buildCategory2($parent, $category) 
$html = "";
if (isset($category['parent_cats'][$parent])) 
    $html .= " <ul id='menu_mb_ul' class='nt_mb_menu'>\n";
    foreach ($category['parent_cats'][$parent] as $cat_id) 
        if (!isset($category['parent_cats'][$cat_id])) 
            $html .= "<li class='menu-item'>\n  <a href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['category_name'] . "</a>\n</li> \n";
        
        if (isset($category['parent_cats'][$cat_id])) 
            $html .= "<li class='menu-item menu-item-has-children only_icon_false'>\n  <a href='" . $category['categories'][$cat_id]['category_link'] . "'><span class='nav_link_txt flex al_center'>" . $category['categories'][$cat_id]['category_name'] . "</span><span class='nav_link_icon ml__5'></span></a><ul class='sub-menu'> \n";
            $html .= "<li class='menu-item'>".buildCategory2($cat_id, $category)."</li>";
            $html .= "</ul> \n";
            $html .= "</li> \n";
        
    
    $html .= "</ul> \n";

return $html;  echo buildCategory2(0, $category);

【讨论】:

以上是关于带有 Php Mysql 的 Css 多级垂直菜单的主要内容,如果未能解决你的问题,请参考以下文章

使用 CSS 和 PHP/MySQL 的动态下拉菜单

带有 HTML 和 CSS 的多级悬停导航栏

带有悬停垂直子菜单的 CSS 水平菜单

带有 jQ​​uery 条件的垂直菜单/子菜单

CSS puer CSS中的多级下拉菜单

前端开发css实战:使用css制作网页中的多级菜单