-1

希望你能帮我

我试图在动态菜单系统中制作

但是我无法使子菜单操作正常工作:(在子菜单保持打开状态之前,您总是必须选择子菜单的一个项目:(

<?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 的新手,我知道,但只有一种方法可以变得更好:)

4

1 回答 1

0

我会用这样的东西:

//Call all menu items from database into array
$dbArray = #whatever database code you choose

parent您可以使用数据库中的字段来分隔顶级链接。

然后让它通过递归函数处理:

function createMenu($dbArray){
  foreach($dbArray as $key => $value){
    if($parent=='top')
     //establish as top link
      createLink($key);
    if($parent=='$id')
     //if parent matches id then add as sub-menu of this top level item
      createLink($key);
  }      
}

function createLink($linkData){
  foreach($linkData as $keys => $values){
    //add <a> and other per-link items
  }
}

我意识到这是一个粗略的答案,但希望它能引导您找到更简单的解决方案!

于 2013-09-14T13:58:29.663 回答