Home > OS >  Php While Loop 3 : 4 Grid
Php While Loop 3 : 4 Grid

Time:03-01

Having some problems with print output like alternative rows 3 : 4 column.

<?php
$i = 1;
while($i=1 < 14){
 if($i <= 3){
   echo $i."<br>";
 } else {
   echo $i."<br>";
 }
 $i  ;
}

?>

** I Want Output Like **

1 2 3
4 5 6 7 
8 9 10 
11 12 13 14

CodePudding user response:

Use the modulo (remainder of a division). Apply a modulo 7 on $i:

for ($i=1; $i<15; $i  ) {
    echo $i, in_array($i % 7, [0, 3]) ? '<br>' : ' ';
}

CodePudding user response:

If you just want to output:

1 2 3
4 5 6 7 
8 9 10 
11 12 13 14

you can just try this,

$firstRow = 0;
$secondRow = 0;
for ($i = 1; $i <= 14; $i  ){
   $firstRow  ; 
   $secondRow  ;

   echo $i . ' ';

   if($firstRow == 3){
      echo '<br>';
      $secondRow = 0;
   }
   if($secondRow == 4){
      echo '<br>';
      $firstRow = 0;
      $secondRow = 0;
   }
}
  • Related