Reputation: 89
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
Upvotes: 0
Views: 57
Reputation: 89584
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>' : ' ';
}
Upvotes: 3
Reputation: 382
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;
}
}
Upvotes: 1