dongming6201 2018-03-29 06:49
浏览 70

在达到极限之前,foreach循环中断

I've been trying to list 9 tables for each student using the following code -

Up to 8 records are getting successfully printed. But for the next value of $s, the next student gets listed.

Where am I getting it wrong?

$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++;
    }
}
  • 写回答

1条回答 默认 最新

  • dongque1646 2018-03-29 06:59
    关注

    try this : $i++ should be after end inner loop.

    <?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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集