计数while循环,在特定循环时间执行其他操作并继续循环

This code print like this. 16 times pictures then 16 times same codes again 16 times pictures then 16 times different codes.

I want that the loop of code load in every loop new code from the database. Not the same code.

Thanks in advance, I could not find any logic for that.

Now it is working. The trick was to insert all the codes to an array. $codes[] = $row['koodi'];

Then print array.

$resturantID = 11; // resturant ID 

$counter = 0; // count amount of loop 

$stmt = $pdo->prepare("SELECT * FROM lappu WHERE resturantID = $resturantID");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $code = $row['code'];
    $counter++;

}

for ($x = 1; $x <= $counter; $x++) {

if (in_array($x, array(17,34,51,68,85,102,119,136,153,170,187,204,221,238,255,272,289,306,323))) {
                    for ($b = 1; $b <= 16; $b++) {
                        echo 'pictures'.'<br>';
                    }

                    for ($b = 1; $b <= 16; $b++) {
                        echo $code.'<br>';
                    }
                }

}
$resId = 11;


$counter = 0;
$ids=array(17,34,51,68,85,102,119,136,153,170,187,204,221,238,255,272,289,306,323);

$stmt = $pdo->prepare( 'select * from `lappu` where `resturantid` = :id' );
$stmt->bindParam( ':id', $resId );
$stmt->execute();

$codes=array();

while( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
    $codes[ $row['resturantid'] ] = $row['code'];
    $counter++;
}








for( $x = 1; $x <= $counter; $x++ ) {
    if ( in_array( $x, $ids ) ) {

        $code=$codes[ $x ];

        for ( $b = 1; $b <= 16; $b++ ) {
            echo 'pictures'.'<br>';
        }

        for ( $b = 1; $b <= 16; $b++ ) {
            echo $code.'<br>';
        } 
    }
}