动态菜单和CSS

时间:2013-09-14 13:27:48

标签: php html mysql css

希望你能帮助我

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

但我无法让子菜单操作正常工作:( 在子菜单保持打开之前,您必须两次选择子菜单的项目:(

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

1 个答案:

答案 0 :(得分: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
  }
}

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