Wrap a div around every four divs

I need a little help in php-loop for OpenCart.

I need to do to wrap a div around data output every 4 cycles.

I have the following

<?php foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php } ?>

I get it

<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>

But I hope it will be

<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>    
</div>

Any help would be greatly appreciated :)

+4
source share
9 answers

try it

<?php 
$i=0;
$wrap_count = 4; // you can change this no of divs to wrap
foreach ($categories as $category) 
{ 
    $i+=1;
    if($i%$wrap_count==1)
    {
        echo '<div class="row">';
    }
?>
    <div class="col-lg-3 col-md-3">.....</div>
<?php 
    if($i%$wrap_count==0)
    {
        echo '</div>';
    }
} 

if($i%$wrap_count!=0)
{
    echo '</div>';
}
?>
+5
source
<?php

$i = 0;
foreach ($categories as $category) {

  if (($i % 4) === 0) {
    echo "<div class='row'>";
  }

  echo '<div class="col-lg-3 col-md-3"></div>';

  if (($i % 4) === 3) {
    echo "</div>";
  }

  $i++;
}

?>

Ps, If it $categoriesis an uncertified array, that is array('a', 'b', 'c'), then you can get rid of i = 0;and i++;and change foreach toforeach ($categories as $i => $category) {


zerkms, % ( Modulo), 0 , 12, reset 0. , . , .

Modulo four 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc

+4

: -

<div class="row">
    <?php
    $i = 1;
   foreach ($categories as $category) { ?>

      <div class="col-lg-3 col-md-3">.....</div>

    <?php if ($i % 4 == 0){ ?>
    </div><div class="row">
    <?php } ?>
    <?php $i++; ?>
    <?php } ?>
+3

. 0. . 0, <div class="row"> 1 . 0 < 5 <div class="col-lg-3 col-md-3">.....</div> 1 . 5 </div> reset 0.

- ( ):

<?php
    $counter = 0;
    foreach ($categories as $category) {
                If ($counter == 0) {
                    echo '<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
';
    $counter++;
                } elseif ($counter > 0 && $counter < 3) {
                    echo '<div class="col-lg-3 col-md-3">.....</div>
';
    $counter++;
                } elseif ($counter == 3) {
                    echo '<div class="col-lg-3 col-md-3">.....</div>
</div>
';
                    $counter = 0;
                }
    }
    if ($counter > 0) {
                echo '</div>
';
    }
?>
+1

Modulo , . , <div>.

div, , 4 3 , </div>.

<?php 

    // set a counter
    $rowCount = 0;

    // store the total number of categories minus 1 as we will be
    // counting from 0 with the counter

    $categoryTotal = count($categories) - 1;

    foreach($categories as $c){

        if($rowCount % 4 == 0){
            echo '<div class="row">';
        }?>

        <div class="col-lg-3 col-md-3""></div>

        <?php 

        // add your closing div if it is the end of a row or if there are no more items    

        if($rowCount % 4 == 3 || $rowCount == $categoryTotal){
            echo '</div>';
        }

        // increment your counter
        $rowCount++;

    } ?>
+1

, :

//before foreach
$i = 0;

// foreach }

$i++;
}//this closes the foreach

div (, foreach:

if($i % 4 == 0)
{
 echo "<div class='each-four-iterations'>";
}

$i ++ :

if($i % 4 == 0)
{
 echo "</div>";
}
0
<?php
$wrap_count = 2; // you can change this no of divs to wrap
foreach ($categories as $category) :
$i+=1;
if($i%$wrap_count==1):
echo '<div class="row">';
endif;?>
<div class="col-lg-3 col-md-3">.....</div>
if($i%$wrap_count==0) : echo '</div>'; endif; endforeach;
?>

0

, .

$new_array = array_chunk($your_array, 4);

foreach($new_array as $group_of_$four){
  echo '<div class="row">';
  foreach($group_of_four as $one){
     // Single Item
  }
  echo '</div>';
}
0

Try it. This works for me.

<?php $count = 1; ?>
                <?php foreach ($model->userProfile->portfolio as $item): ?>
                    <?php if ($count % 4 == 1): ?>
                        <div class="row" style="margin-bottom: 30px;">
                    <?php endif ?>

                        <div class="col-md-3">

                        </div>

                    <?php if ($count % 4 == 0): ?>

                        </div>

                    <?php endif ?>

                    <?php  $count++ ?>
                <?php endforeach ?>
0
source

All Articles