Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap columns two and two inside of a loop?

Tags:

loops

php

I'm trying to wrap two columns inside of a row div from inside a php loop.

I came up with this test but it fails no matter what I do.. This is the closest I can come, I can't see where this logic fails.

Take a look:

<?php $i = 0; ?>
<?php while($i < 11) : ?>

  <?php if ($i % 2 === 0) : ?>

    <div class="row">row

  <?php endif; ?>

  <span><?php echo "[" . $i . "]"; ?></span>

  <?php if (!$i % 2 === 0) : ?>

     /row
    </div>

  <?php endif; ?>

  <?php $i++; ?>
<?php endwhile; ?>

The produced result:

row [0] [1] /row
row [2] /row
[3] /row
row [4] /row
[5] /row
row [6] /row
[7] /row
row [8] /row
[9] /row
row [10] /row

Here we can see that the first row is working great but then it becomes flawed somewhere in the logic, question is where?

like image 654
Chrillewoodz Avatar asked Nov 17 '25 18:11

Chrillewoodz


1 Answers

Made a little changes to your code.

  <?php $i = 0; 
       while($i <= 11) :  
       if($i%2==0){
        echo '<div class="row"> Row';
       } 
         echo "[" . $i . "]"; 

       if($i%2!=0){
        echo " row </div>";
       }
       $i++; 
     endwhile; ?>

It will output

 Row[0][1] row
 Row[2][3] row
 Row[4][5] row
 Row[6][7] row
 Row[8][9] row
 Row[10][11] row
like image 66
Meenesh Jain Avatar answered Nov 19 '25 10:11

Meenesh Jain