【问题标题】:Dynamic menu and css动态菜单和 CSS
【发布时间】:2013-09-14 21:58:29
【问题描述】:

希望你能帮助我

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

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

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

【问题讨论】:

  • 你可以在plaq.dk/classes/sidebar.class.php看到它的直播
  • 那是一堆乱七八糟的代码。查看递归函数。至于“你必须选择子菜单中的一个项目两次”,一旦你的实际菜单建立起来,剩下的就是由 CSS 来实际隐藏/显示子菜单。
  • 问题不是 CSS,而是我的函数“修改菜单”通过 Session 设置“子菜单”的部分

标签: php html mysql css


【解决方案1】:

我会选择这样的:

//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
  }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多