I want to use a counter as a custom in my while loop, let’s say we have a while loop < 21 I am trying to output something like this results:
1,2,
3,4,
5,6,7,
8,9,
10,11,
12,13,14,
15,16,
17,18,
19,20,21
I tried with writing these codes
<?php
$counter = 1;
while ($counter <21) :
if( $counter == 1 || ($counter-1)%4 == 0 ) {
echo $counter."<br>";
}
if( $counter == 2 || ($counter-2)%4 == 0 ) { ?>
<div class="others">
<?php }
if( $counter != 1 && ($counter-1)%4 != 0 ) {
echo $counter.',';
}
if( $counter%4 == 0 ) { ?>
</div>
<?php }
$counter++;
endwhile;
But the output is like this
1
2,3,4,
5
6,7,8,
9
10,11,12,
13
14,15,16,
17
18,19,20,
How can I do such a thing?