1

I'm using bootstrap and need to create structure like this

    <div class="row">
        <div class="col-sm-3">
            <!--content loop 1-->
        </div>
        <div class="col-sm-3">
            <!--content loop 1-->
        </div>
        <div class="col-sm-3">
            <!--content loop 2-->
        </div>
        <div class="col-sm-3">
            <!--content loop 2-->
        </div>
    </div>

    <div class="row">
        <div class="col-sm-3">
            <!--content loop 3-->
        </div>
        <div class="col-sm-3">
            <!--content loop 3-->
        </div>
        <div class="col-sm-3">
            <!--content loop 4-->
        </div>
        <div class="col-sm-3">
            <!--content loop 4-->
        </div>
    </div>

I need to create it in a single foreach loop but on each loop I only create this structure.

    <div class="col-sm-3">
        <!--content loop 1-->
    </div>
    <div class="col-sm-3">
        <!--content loop 1-->
    </div>

I can create the first <div class="row"></div> outside of the loop but how can I break out of the loop to create the second <div class="row"></div> and continue the loop

3 Answers 3

4

You can do some thing like this. You can modify according to your need. I just mentioned logic

   <div class="row">
     <?php
         $i = 0;
         foreach(....../*your code*/){
           if($i == 4){
               $i = 0;
              ?>
                  </div><div class="row">
                <?php
            } 
                ?>
            <div class="col-sm-3">
               <!--content-->
            </div>
          <?php
            $i++;
         }
        ?>
        </div>
Sign up to request clarification or add additional context in comments.

2 Comments

I thought this was working but it seems to miss or skip the 3rd position in the array - I have a demo here viper-7.com/EsGoc9. I want 2 divs in each row and two rows. I have just output the array elements and it misses the 3rd element
yes it seems i missed it. see the edit i made in answer. and check edit here viper-7.com/bRjxb3
1

It's as simple as following this logic:

for($i = 0;$i < 4;$i++) {
    echo '<div class="row">';
    for($j = 0;$j < 4;$j++) {
        echo '<div class="col-sm-3"></div>';
    }
    echo '</div>;'
}

Comments

0

I like to do this way because i think is cleaner and easier to manage:

<?php
    $colsOffset = 4;
    $items = array('content1','content2','content3','etc...');
    $i = 0;
?>
<?php foreach ( $items as $item ) { ?>
    <?php echo ($i % $colsOffset == 0 || $i == 0) ? '<div class="row">' : '' ?>
    <div class="col-sm-3">
       <!--content loop-->
    </div>
    <?php echo (($i + 1) % $colsOffset == 0 || ($i + 1) == count($items)) ? '</div>' : '' ?>
    <?php $i++; ?>
<?php } ?>

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.