重用while循环仅适用于一个选择框

时间:2019-03-27 23:53:55

标签: php html while-loop

我需要从页面顶部的杯子选择框生成一组球队,此代码允许我运行while循环以为第一个选择框生成正确的结果,但是,当我复制代码时在第二个显示没有结果?

      <form class="seed-form">
        <select name="team" required>
        <option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
          <?php
          while ($team = mysqli_fetch_assoc($show_teams)) { ?>
              <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
          <?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
        </select>
        <select name="team" required>
        <option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
          <?php
          while ($team = mysqli_fetch_assoc($show_teams)) { ?>
              <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
          <?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
        </select>



        <input type="submit" name="submit" value="Generate" class="submit">
          <?php

          if (isset($_POST['submit'])) { //checking if submit button was clicked

            include_once 'action/dbcon.php';

            $cname = $_POST['cupname'];

            if (empty($cname)) {
                header("Location: tables.php?field=empty"); //return them if fields are empty
                exit();
            } else {
                $sql = "SELECT * FROM teams WHERE cup_name='$cname'";
                $show_teams = mysqli_query($conn, $sql);
                $numberCheck = mysqli_num_rows($show_teams);

                if ($numberCheck < 8) {
                header("Location: tables.php?tables=1"); //Take to cup page if there arent enough teams in the cup
                }
            }
          }
          ?>

1 个答案:

答案 0 :(得分:0)

问题是fetch_assoc()返回结果集中的下一行,除非没有剩余(在这种情况下,它返回NULL-参见docs)。

您的第一个while循环将运行,直到没有更多结果为止。因此,当您开始第二个while循环时,仍然...您猜到了:没有更多结果了。您已经用光了。因此,第二个循环中对fetch_assoc()的第一次调用会立即返回NULL,因此永远不会满足循环条件,并且循环也不会执行。

有两种解决方法:

1)将结果指针重置为结果集的开头。在两个循环之间的某个地方写

mysqli_data_seek($show_teams, 0);

有关更多信息,请参见docs

2)将所有数据读取到一个PHP数组中,然后您可以使用foreach遍历任意次数:

      <?php
      $teams = array();
      while ($team = mysqli_fetch_assoc($show_teams)) {
        $teams[] = $team;
      }
      ?>

      <form class="seed-form">
        <select name="team" required>
        <option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
          <?php
          foreach ($teams as $team) { ?>
              <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
          <?php } ?><!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
        </select>
        <select name="team" required>
        <option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
          <?php
          foreach ($teams as $team) { ?>
              <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
          <?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
        </select>