PHP I need a for loop to go through array a few times

41 views Asked by At

I want to fetch a few rows of data from a MySQL database using PHP. I have 5 different icons, and I want them to be displayed next to every item in a specific order. If there are more than 5 rows in the Database I want the 6th row to have the first icon, the 7th the 2nd, and so on.

1

There are 1 answers

0
iMakeWebsites On BEST ANSWER

Some structure like this would work

$count = 1;
while(){
    if($count % 5 == 1){
        //1,6,11,etc
    }
    if($count % 5 == 2){
        //2,7,12,etc
    }
    if($count % 5 == 3){
        //3,8,13,etc
    }
    if($count % 5 == 4){
        //4,9,14,etc
    }
    if($count % 5 == 0){
        //5,10,15,etc
    }
    $count++;
}

Or maybe even better would be:

$icons = array("icon5.png","icon1.png","icon2.png","icon3.png","icon4.png");
$count = 1;
while(){
    print $icons[($count % 5)];
    $count++;
}