2

I need to add a div tag after each 5 items. can any one provide any assistance with this logic, i have tried but, no success.

I have this code:

<div class="product-grid">
    <?php foreach ($products as $product) { ?>
    <div class="item">.....</div>
    <?php } ?>
  </div>

I get this

<div class="product-grid">
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
 </div>

It would look like this: (add a div after 5 item in sequence.)

<div class="product-grid">
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="clearfix visible-xs-block"></div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="item">.....</div>
    <div class="clearfix visible-xs-block"></div>
    <div class="item">.....</div>
 </div>

Some help would be appreciated.

2
  • put a counter inside the loop and if the counter is dividable with 6, add the required div, or else add the normal div.
    – Vucko
    Commented Aug 2, 2014 at 12:10
  • please check :- stackoverflow.com/questions/25093916/…
    – Khushboo
    Commented Aug 2, 2014 at 12:14

4 Answers 4

5
<?php 
// control variable 
$counter = 0;
?>
<div class="product-grid">
    <?php foreach ($products as $product) { ?>
        <div class="item">.....</div>
        <?php 
        // on every #5 result
        if(++$counter % 5 === 0) { ?>
          <div class="clearfix visible-xs-block"></div>
        <?php  
        }
    } 
    ?>
  </div>
0
0

Use the MOD operator:

<div class="product-grid">
    <?php
        $i = 0;
        foreach ($products as $product) {
            $i++;
            if($i % 5 == 0){
    ?>
                <div class="clearfix visible-xs-block"></div>
    <? } ?>
        <div class="item">.....</div>
    <?php } ?>
</div>
0

Since, it's every sixth item getting that particular class, you can use a temporary counter to keep track:

<div class="product-grid">
    <?php
      $i = 1;
      foreach ($products as $product) {
        if( $i % 6 != 0 ) {
    ?>
    <div class="item">.....</div>
    <?php
        }
        else {
    ?>
    <div class="clearfix visible-xs-block"></div>
    <?php
        }
        $i++;
      }
    ?>
</div>
0
You can do it like this:

<div class="product-grid">
   <?php
        $i = 1;
        foreach ($products as $product) {
            echo '<div class="item">.....</div>';
          if($i%5 == 0){
              echo '<div class="clearfix visible-xs-block"></div>';
          }  
          $i++;
        }
   ?> 
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.