php - 计数while循环,在特定的循环时间内做其他事情并继续while循环
问题描述
这段代码是这样打印的。16 次图片然后16 次相同代码再 16 次图片然后 16 次不同代码。
我希望代码循环在每个循环中加载来自数据库的新代码。不一样的代码。
在此先感谢,我找不到任何逻辑。
现在它正在工作。诀窍是将所有代码插入到数组中。$codes[] = $row['koodi'];
然后打印数组。
$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>';
}
}
}
推荐阅读
- python - 我想为那些小睡到一个新变量的人以及那些没有小睡到另一个新变量的人隔离“夜间就寝时间”列
- java - Android Studio 中操作栏的 java.lang.NullPointerException
- php - 找不到类“App\Http\Controllers\rimdetails”?
- php - 从数组中提取一个数字
- python-3.x - “数据框到谷歌电子表格”类型错误:“数据框”对象是可变的,因此它们不能被散列
- java - JDBC 模板更新方法内部行为
- javascript - 在角度项目中编辑单个表单数据字段
- python - Tkinter 类函数在尝试使用入口小部件时导致错误
- javascript - 路由端点未路由,出现引用错误
- node.js - 在 EJS 中渲染多个项目