在基于MySQL结果的PHP while循环中,每隔1次和4次运行一次脚本
我有一个在基于MySQL结果的PHP while循环中,每隔1次和4次运行一次脚本,php,mysql,loops,while-loop,Php,Mysql,Loops,While Loop,我有一个while循环从MySQL表中获取数据。返回的行数未知。我希望在每个第1项和第4项上运行循环中的部分代码,如: while ($result = $stmt->fetch()) { //Run on first row always, then repeat on the 4th item and so on. echo '<div class="row">'; } while($result=$stmt->fetch()){ //始终在第一行运行,然
while
循环
从MySQL
表中获取数据。返回的行数未知。我希望在每个第1项和第4项上运行循环中的部分代码,如:
while ($result = $stmt->fetch()) {
//Run on first row always, then repeat on the 4th item and so on.
echo '<div class="row">';
}
while($result=$stmt->fetch()){
//始终在第一行运行,然后在第四项上重复,依此类推。
回声';
}
我如何才能做到这一点?//跟踪迭代计数
// keep track of the iteration count
$i = 1;
while ($result = $stmt->fetch()) {
//if it's the first iteration, open the row
if($i == 1) { echo '<div class="row">'; }
//...
// NOTE: count($results) is pseudocode, make sure you provide it somehow
// if you're on the 4nth or the last iteration
if($i % 4 == 0 or $i == count($results))
{
// close the row
echo '</div>';
// if it's not the last item in the loop, open a new row
if($i != count($results)) { echo '<div class="row">'; }
}
$i++;
}
$i=1;
而($result=$stmt->fetch()){
//如果是第一次迭代,请打开该行
如果($i==1){echo';}
//...
//注意:count($results)是伪代码,请确保以某种方式提供它
//如果您在第4次迭代或最后一次迭代中
如果($i%4==0或$i==count($results))
{
//关门
回声';
//如果它不是循环中的最后一项,请打开新行
如果($i!=count($results)){echo';}
}
$i++;
}
带有存储循环的标志变量的简单if/elseiteration@MKhalidJunaid你能详细说明一下吗?$results
来自哪里?正如评论所说,这是伪代码。我不知道结果来自哪里,也不知道如何计算。我现在说得更清楚了,谢谢!正是我需要的。