3

I use this code to create a bootstrap dropdown for an infinte menu

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

But The output comes as

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

All the menu item with parent_menu_id = 0 gets listed as menu , instead I need everything listed under one menu 'Categories'

1 Answer 1

2
 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.