Php foreach循环在达到极限之前中断
我一直在尝试使用以下代码为每个学生列出9个表- 最多可成功打印8条记录。但是对于下一个值$s,将列出下一个学生 我哪里弄错了Php foreach循环在达到极限之前中断,php,mysql,foreach,Php,Mysql,Foreach,我一直在尝试使用以下代码为每个学生列出9个表- 最多可成功打印8条记录。但是对于下一个值$s,将列出下一个学生 我哪里弄错了 $query="SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC"; $Result = mysqli_query($conn, $query); $s=1; while($row=mysqli_fetch_array($Result)) { foreach($row
$query="SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s=1;
while($row=mysqli_fetch_array($Result)) {
foreach($row as $field) {
if($s==10){
$s=1; break;
} else if($s==1){
$sub="English (WS VOL-1)";
} else if($s==2){
$sub="English (WS VOL-2)";
} else if($s==3){
$sub="English (WS VOL-3)";
} else if($s==4){
$sub="Maths (WS VOL-1)";
} else if($s==5){
$sub="Maths (WS VOL-2)";
} else if($s==6){
$sub="Maths (WS VOL-3)";
} else if($s==7){
$sub="Science (WS VOL-1)";
} else if($s==8){
$sub="Science (WS VOL-2)";
} else if($s==9){
$sub="Science (WS VOL-3)";
}
echo $table;
$s++;
}
}
试试这个:$i++应该在结束内部循环之后
<?php
$query = "SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s = 1;
while ($row = mysqli_fetch_array($Result)) {
foreach ($row as $field) {
if ($s == 10) {
$s = 1;
break;
}
if ($s == 1) {
$sub = "English (WS VOL-1)";
} elseif ($s == 2) {
$sub = "English (WS VOL-2)";
} elseif ($s == 3) {
$sub = "English (WS VOL-3)";
} elseif ($s == 4) {
$sub = "Maths (WS VOL-1)";
} elseif ($s == 5) {
$sub = "Maths (WS VOL-2)";
} elseif ($s == 6) {
$sub = "Maths (WS VOL-3)";
} elseif ($s == 7) {
$sub = "Science (WS VOL-1)";
} elseif ($s == 8) {
$sub = "Science (WS VOL-2)";
} elseif ($s == 9) {
$sub = "Science (WS VOL-3)";
}
echo $table;
}
$s++; //this is only change
}
什么是$table
?这是一个HTML表格,我一直在重复。代码有点太长了,所以我用一个变量代替了它。我真的不知道你的代码是用来做什么的,但你的意思可能是have$s=1代码>在每个外部循环的开始处重置计数器。似乎您有一个无限循环。当$s==10
时,将$s
重置为1
,然后etc@AkintundeOlawale它不是无限的,当$s点击10时会有一个中断,while循环也会结束。这次仍然有8条记录只打印第一个主题:(问题在于while循环,我认为每个学生只打印8张表格。为什么会这样。。?