Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap a div around every four divs

Tags:

php

opencart

I need a little help with a php loop for OpenCart.

I need to do is wrap a div around the output of the data every 4 loops.

I have the following

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

I get this

<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 what I'm hoping to get is

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

like image 201
HDP Avatar asked Aug 02 '14 10:08

HDP


4 Answers

try this

<?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>';
}
?>
like image 70
Satish Sharma Avatar answered Nov 19 '22 14:11

Satish Sharma


<?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 $categories is a non keyed array, ie array('a', 'b', 'c') then you can get rid of the i = 0; and i++; and change the foreach to foreach ($categories as $i => $category) {


And as zerkms mentioned in a comment, the magic here is coming from % (Modulo operation), which is essentially like a clock- the numbers will increment from 0 upwards until they hit 12, at which point it is reset to 0 again. So the clock is said to be modulo twelve. In this instance we're using modulo four to cyclically print our our opening and closing row elements.

Modulo four would be the sequence 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc

like image 4
Ashish Avatar answered Nov 19 '22 14:11

Ashish


Try this :-

<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 } ?>
like image 3
Khushboo Avatar answered Nov 19 '22 13:11

Khushboo


You may use a counter variable. Initially set it to equal 0. Then for each loop check its value. If it is equal to 0 then output <div class="row"> and add 1 to the variable's value. If its value is > 0 and < 5 then output <div class="col-lg-3 col-md-3">.....</div> and add 1 to it's value. If it is equal to 5 output </div> and reset the counter variable to 0.

Something like (code tested):

<?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>
';
    }
?>
like image 1
Zeno Avatar answered Nov 19 '22 14:11

Zeno