PHP foreach loop, each number of steps creates a new outer loop wrapper

advertisements

I need some advice on this one. If I have a PHP foreach loop:

<div class="wrapper">
  <?php foreach ($item as $element): ?>
    <!-- some HTML of $element -->
  <?php endforeach; ?>
</div>

and after every 5th $item I want to create a new .wrapper with the next 5 items in the foreach. And redo this step until all are through. The output should be like:

<div class="wrapper">
  <!-- some HTML of $element 1 -->
  <!-- some HTML of $element 2 -->
  <!-- to $element 5 -->
</div>

<div class="wrapper">
  <!-- some HTML of $element 6 -->
  <!-- to $element 10 -->
</div>

Do I need to run another foreach outside to make this possible?

Thanks


Try this

$i = 0;
<?php
    foreach ($item as $element) {
        if($i%5==0) echo "<div class=\"wrapper\">";
?>

        <!-- some HTML of $element -->

<?php
        if($i%5==4) echo "</div>";
        $i++;
    }
?>