0

I have my menu styled with Bootstrap. My dropdown menu was like that with html+bootsrap:

enter image description here`

<div class="container">
   <div class="row">
      <ul class="col-sm-3">
         <li>item</li>
         <li>item</li>
         <li>item</li>
         <li>item</li>                              
      </ul>
      <ul class="col-sm-3">
         <li>item</li>
         <li>item</li>
         <li>item</li>
         <li>item</li>                              
      </ul>
      <ul class="col-sm-3">
         <li>item</li>
         <li>item</li>
         <li>item</li>
         <li>item</li>                  
      </ul>
      <ul class="col-sm-3">
         <li>item</li>
         <li>item</li>
         <li>item</li>
         <li>item</li>              
      </ul>
   </div>
</div>

I used the class col-sm-3 every 4 items to achieve that.

But i cant do that in php cause i am using foreach loop and the result is like this one:enter image description here

I tried with col-sm-3 but not..

    <div class="collapse navbar-collapse btnCollapse">
      <ul class="nav navbar-nav">
        <li><a href="#">Home</a></li>
        <?php foreach ($navItems as $item) { ?>
        <li class="dropdown">
            <a href="<?php echo $item['slug']; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $item['title']; ?><span class="caret"></span></a>
            <?php if(isset($item['dropdown'])){ ?>
               <ul class="dropdown-menu" role="menu"> 
                   <div class="container">
                      <div class="row">
                          <ul class="col-sm-3">
                            <?php foreach($item['dropdown'] as $subitem) { ?>
                               <li><a href="<?php echo $subitem['slug'] ?>"><?php echo $subitem['title'] ?></a></li>
                            <?php }; ?>
                          </ul>
                      </div>
                   </div>
               </ul>                     
         <?php   }; ?>
        </li>
        <?php }; ?>
      </ul>
    </div>

WHats the way to achieve that?

Maybe should I use For loop instead of foreach? I dont know..

Any feedback?

5
  • please post your output HTML as well.. From your current code and output I can assume that you're having one loop for all menu items while in Desired output HTML have multiple parent-child menu items. Commented May 22, 2017 at 10:15
  • The 1st image is my output of html, that the dropdown and when i turn it to php loop is like the 2nd one Commented May 22, 2017 at 10:17
  • You need extra css codes. here is a example bootsnipp.com/snippets/featured/large-dropdown-menu Commented May 22, 2017 at 10:19
  • @MehmetSoylu i have it.. just the col-sm-3 in not applied to the array i have for the dropdown menu Commented May 22, 2017 at 10:20
  • @Maria, Can you try my answer? Commented May 22, 2017 at 10:23

1 Answer 1

1
<div class="collapse navbar-collapse btnCollapse">
      <ul class="nav navbar-nav">
        <li><a href="#">Home</a></li>
        <?php foreach ($navItems as $item) { ?>
        <li class="dropdown">
            <a href="<?php echo $item['slug']; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $item['title']; ?><span class="caret"></span></a>
            <?php if(isset($item['dropdown'])){ ?>
               <ul class="dropdown-menu" role="menu"> 
                   <div class="container">
                      <div class="row">
                        <?php $i = 0; foreach($item['dropdown'] as $subitem) { 
                          if($i%4==0){ 
                             echo "<ul class="col-sm-3">";
                           } ?>
                               <li><a href="<?php echo $subitem['slug'] ?>"><?php echo $subitem['title'] ?></a></li>
                          <?php if($i%4==3){ 
                          echo "</ul>";
                          }
                          $i++;
                        }; ?>
                      </div>
                   </div>
               </ul>                     
         <?php   }; ?>
        </li>
        <?php }; ?>
      </ul>
    </div>

In your desired output you need 4 colums, so I devided menu items by 4 to put column i.e. if($i%4==0) for starting <ul> and if($i%4==3) for ending </ul>.

Sign up to request clarification or add additional context in comments.

2 Comments

Shaunak is working just for the last 4... Not for the whole dropdown
@Maria, I have updated my answer just now please check that.

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.