Skip to content
Advertisement

php add a counter in while loop and return to a new line when i reach this counter

this is my code:

$q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
while($row=mysqli_fetch_array($q)){

    if($row['id-team']==$_POST["teamm"]){
        if($row['year-st']==$_SESSION["year"]){

            $p=mysqli_query($idcon,"SELECT * FROM `subscription`");

            while($row1=mysqli_fetch_array($p)){
                if($row1['id-subs']==$row['id-subs'])
                    echo"<img src='".$row1[8]."'/>";
            }
        }
    }
}

now this code works fine, but it echo my results on the same line since its a loop.

i want my output to be 3 photos per line for example: image1 image2 image3 (jumps to line) image4 image5 image6 and not: image1 image2 image3 image4 image5 image6. To be more precise i want to add a condition that when the loop reaches 3 values, it returns to a new line and continue printing the results.

Advertisement

Answer

Try this. not tested but should work as your need.

    $q=mysqli_query($idcon,"SELECT * FROM `subscribed-team`");
        while($row=mysqli_fetch_array($q)){

            if($row['id-team']==$_POST["teamm"]){
                if($row['year-st']==$_SESSION["year"]){

                    $p=mysqli_query($idcon,"SELECT * FROM `subscription`");
                    $counter = 0; 
                    while($row1=mysqli_fetch_array($p)){
                        if($row1['id-subs']==$row['id-subs'])
                            echo"<img src='".$row1[8]."'/>";
                            $counter++; 
                            if($counter == 2){ 
                               echo "<br>";
                               $counter = 0;
                            }
                    }
                }
            }
        }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement