【发布时间】:2013-06-16 01:31:56
【问题描述】:
<?php
// Select all entries from the menu table
$sql1 = $pardConfig->prepare("SELECT id, menu_title, menu_link, parent FROM pard_menu ORDER BY parent, sort, menu_title");
// Create a multidimensional array to conatin a list of items and parents
$sql1->execute();
$menu = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
while ($items = $sql1->fetch()) {
// 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'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($pardConfig, $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]['menu_link'] . "'>" . $menu['items'][$itemId]['menu_title'] . "</a>\n</li> \n";
}
if (isset($menu['parents'][$itemId])) {
$html .= "
<li>\n <a href='" . $menu['items'][$itemId]['menu_link'] . "'>" . $menu['items'][$itemId]['menu_title'] . "</a> \n";
$html .= buildMenu($pardConfig, $itemId, $menu);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
}
echo buildMenu($pardConfig, 0, $menu);
?>
上面的代码包含一个用于创建具有多级子菜单的动态菜单的 php 代码。我为此预定义了类...
- 对于主
ul(第一 Ul)我有.nav类 - 对于任何有一级子菜单的
li,我有.dropdown类 为李 - 除了第二步,我还有
.dropdown-menu类ul元素 它的父元素为li - 我有一个用于 3 级子菜单的
.dropdown-submenu类
所以我想修改我的代码,将这 4 个设置添加到其中?任何帮助将不胜感激?
我将为此使用引导程序菜单 API
我用 Jquery 完成了这种方法,如下所示。但它不是 100% 好..
$(".nav-collapse").find("ul").first().addClass("nav");
$(".nav-collapse").find("li").has("ul").addClass("nav");
$(".nav").find("li:has(ul) a").attr("data-toggle", "dropdown");
$('ul').filter(function () {
return $(this).parent().is('li')
}).addClass('dropdown-menu');
$(".nav").find("li ul li").addClass("dropdown-submenu");
$('.dropdown-toggle').dropdown();
});
【问题讨论】:
-
那么您的代码到底有什么问题?
-
需要在 php 中添加这样一个类。这对我来说很难做到