希望你能帮我
我试图在动态菜单系统中制作
但是我无法使子菜单操作正常工作:(在子菜单保持打开状态之前,您总是必须选择子菜单的一个项目:(
<?php
include '../inc/db_connect.php';
// 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
$sql = <<<SQL
SELECT id, label, link, parent, class
FROM menu
ORDER BY parent, sort, label
SQL;
if(!$result = $mysqli->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
$menu = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the menu table
while ($items = mysqli_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'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
$menu = call_user_func(modifymenu, $parent,$menu);
$html = "";
if (isset($menu['parents'][$parent]))
{
$html .= "
<ul>\n";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]))
{
$html .= "<li class ='".$menu['items'][$itemId]['class']."'>\n <a href='?p=".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
if(isset($menu['parents'][$itemId]))
{
if ($_SESSION['submenu'] == $menu['items'][$itemId]['id'] ) {
$menu['items'][$itemId]['class'] = "active open";
}
$html .= "
<li class ='submenu ".$menu['items'][$itemId]['class']." '>\n <a href='?p=".$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);
function modifymenu ($parent, $menu) {
$ref = isset($_GET['p']) ? $_GET['p'] : null;
foreach ($menu['parents'][$parent] as $itemId)
{
if($ref == $menu['items'][$itemId]['link'] ) {
$menu['items'][$itemId]['class'] = "active";
if (isset($menu['items'][$itemId]['parent'])) {
$_SESSION['submenu'] = $menu['items'][$itemId]['parent'];
}else{
$_SESSION['submenu'] = '';
}
}
}
return $menu;
}
function modifyparent ($parent, $menu) {
$ref = isset($_GET['p']) ? $_GET['p'] : null;
foreach ($menu['parents'][$parent] as $itemId)
{
if($ref == $menu['items'][$itemId]['link'] ) {
$menu['items'][$itemId]['class'] = "active";
$_SESSION['submenu'] = $menu['items'][$itemId]['parent'];
}
}
return $menu;
}
希望有人可以阻止错误
PS。我是 php 的新手,我知道,但只有一种方法可以变得更好:)