How to output a custom while loop php

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?

Before answering your question, I have to ask why. I don’t see any logic or reason for doing such a thing.

What is the real problem you are trying to solve? This makes no sense as posted.

I can’t think of any real world use for such code. Though I guess it could serve as a logic learning exercise.

Maybe it would help to add comments that let you visualize what happens where under what conditions?

while ($counter <21) : 
    if( $counter == 1 || ($counter-1)%4 == 0 ) { 
// 1, 5, 9, 13, 17 echo 
        echo $counter."<br>";
    } 

    if( $counter == 2 || ($counter-2)%4 == 0 ) { ?> 
// 2, 6, 10, 14, 18 open div tag 
    <div class="others">
    <?php } 
    if( $counter != 1 && ($counter-1)%4 != 0 ) { 
// 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20  echo 
        echo $counter.',';
    }
    if( $counter%4 == 0 ) { ?> 
// 4, 8, 12, 16, 20 close div tag 
        </div>
    <?php } 
    $counter++;
endwhile;

I am trying to loop a list of posts from a database to create something like this…

Thank’s bro I have solved it ^^ …

<?php
$counter = 1;
$module =  7;
$i = 2;
$arr = [];
while ($counter <22) : 
    if( $counter == 1 || ($counter-1)% $module == 0  ) { 
        echo $counter.',';
    } 
    if(  $counter == 2  || ($counter-2)% $module    == 0 ) { 
        echo $counter.',<br>';
    } 
    if( $counter == 3 || ($counter-3)% $module == 0  ) { 
        echo $counter.',';
    } 
    if(  $counter == 4  || ($counter-4)% $module    == 0 ) { 
        echo $counter.',<br>';
    }
    $i = $i + $module;
    $arr[] = $i;
    if(!in_array($counter,$arr)){
        if( $counter == 5 || ($counter-5)% $module == 0 ) { 
            } 
        if( $counter != 1 && $counter != 2  && $counter != 3 && ($counter-1)% $module != 0  
        && $counter != 4  && ($counter-3)% $module != 0 && ($counter-4)% $module != 0  ) { 
            echo $counter.',';
        }
        if( $counter% $module == 0 ) { 
            echo '<br>';
        }
    } 
    $counter++;
endwhile; 
1 Like

The Real problem which you have now revealed is a design layout problem, not a programming problem. Try viewing your page in different view ports. (Phone, tablet, widescreen). The correct answer is going to lie in CSS and proper breakpoints. You will want to learn CSS Grid or Flex or both even.

FYI: Your OP is known as an XY Problem. It is basically asking about your attempted solution rather than the real problem. http://xyproblem.info/

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.