2013-12-16 105 views
3

我使用此代碼來創建一個infinte菜單PHP遞歸引導下拉菜單

<ul class="nav"> 

          <?php 
       $menu_items = array(
       (object) array('id'=>'1', 'name'=>'Electronics', 'parent_menu_id'=>'0'), 
       (object) array('id'=>'2', 'name'=>'Books', 'parent_menu_id'=>'0'), 
       (object) array('id'=>'3', 'name'=>'Toys', 'parent_menu_id'=>'0'), 
       (object) array('id'=>'4', 'name'=>'Kitchen', 'parent_menu_id'=>'0'), 
       (object) array('id'=>'5', 'name'=>'Apparel', 'parent_menu_id'=>'0'), 
       (object) array('id'=>'6', 'name'=>'Shirts', 'parent_menu_id'=>'5'), 
       (object) array('id'=>'7', 'name'=>'Pants', 'parent_menu_id'=>'5'), 
       (object) array('id'=>'8', 'name'=>'Hats', 'parent_menu_id'=>'5'), 
       (object) array('id'=>'9', 'name'=>'Gloves', 'parent_menu_id'=>'5'), 
       (object) array('id'=>'10', 'name'=>'Ballcaps', 'parent_menu_id'=>'8'), 
       (object) array('id'=>'11', 'name'=>'Beanies', 'parent_menu_id'=>'8'), 
       (object) array('id'=>'12', 'name'=>'Wool', 'parent_menu_id'=>'11'), 
       (object) array('id'=>'13', 'name'=>'Polyester', 'parent_menu_id'=>'11'), 
       (object) array('id'=>'14', 'name'=>'Jimsider.com', 'parent_menu_id'=>'4'), 
      ); 

       global $menuItems; 
       global $parentMenuIds; 
       //create an array of parent_menu_ids to search through and find out if the current items have an children 
       foreach($menu_items as $parentId) 
       { 
        $parentMenuIds[] = $parentId->parent_menu_id; 
       } 
       //assign the menu items to the global array to use in the function 
       $menuItems = $menu_items; 

       //recursive function that prints categories as a nested html unorderd list 
       function generate_menu($parent) 
       { 
         $has_childs = false; 

         //this prevents printing 'ul' if we don't have subcategories for this category 
         global $menuItems; 
         global $parentMenuIds; 
         //use global array variable instead of a local variable to lower stack memory requierment 
         foreach($menuItems as $key => $value) 
         { 
          if ($value->parent_menu_id == $parent) 
          {  
           //if this is the first child print '<ul>' 
           if ($has_childs === false) 
           { 
            //don't print '<ul>' multiple times 
            $has_childs = true; 
            if($parent != 0) 
            { 
            echo '<ul class="dropdown-menu">'; 
            } 
           } 

           if($value->parent_menu_id == 0 && in_array($value->id, $parentMenuIds)) 
           { 
            echo '<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">' . $value->name . '<b class="caret"></b></a>'; 
           } 
           else if($value->parent_menu_id != 0 && in_array($value->id, $parentMenuIds)) 
           { 
            echo '<li class="dropdown-submenu"><a href="#">' . $value->name . '</a>'; 
           } 
           else 
           { 
            echo '<li><a href="#">' . $value->name . '</a>'; 
           } 
           generate_menu($value->id); 

           //call function again to generate nested list for subcategories belonging to this category 
           echo '</li>'; 
          } 
         } 
         if ($has_childs === true) echo '</ul>'; 
       } 
       generate_menu(0); 
       ?> 
    <!-- End Dynamic Nav --> 
     </li> 
      </ul> 

自舉下拉,但輸出來作爲

<ul class="nav"> 
<li><a href="#">Electronics</a></li> 
<li><a href="#">Books</a></li> 
<li><a href="#">Toys</a></li> 
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Kitchen<b class="caret"></b></a> 
    <ul class="dropdown-menu"> 
     <li><a href="#">Jimsider.com</a></li> 
    </ul> 
</li> 
....... 
</ul> 

所有與parent_menu_id = 0的菜單項被列如菜單,而不是我需要一個菜單​​中列出的一切「分類」

回答

1
function toULlistproduct($arr) 
{ 
    $html = '<ul>'.PHP_EOL; 
    foreach ($arr as $k => $v) 
    {   
      if(is_array($v)){   
       $html .= '<li><a href="#" id="' . $k . '" >'.$k.'</a></li>' ; 
       $html .= toULlistproduct($v); 
      }else{ 
       $html .= '<li><a href="#" id="' . $k . '" >'.$v.'</a></li>' ; 
      } 
    } 
    $html .= '</ul>'.PHP_EOL; 
    return $html; 
} 
$arr = array(
    'a' => array(1,2,3,4,5,6,7,8,9,...), 
); 
echo toULlistproduct($arr);