在 php 和 mysqli 中为一个数组创建多个 while 循环

Creating multiple while loops for one array in php and mysqli

所以我正在尝试创建一个列表来显示属于某个区域的所有用户。为此,我尝试使用 while 循环来显示所有名称。请参阅下面的代码:

<h2>Region 2:</h2>
<?php 

          //REGION 2:
          $result = mysqli_query($ExampleDB,"SELECT * FROM members WHERE region=2 ORDER BY user DESC");

            while($row = mysqli_fetch_array($result))
              {
              echo "</br>Position 2: ";
              if ($row['position']==2){
              echo"".$row['fname']." ".$row['sname'].", ";
              }
              echo "</br>Position 3: ";
              if ($row['position']==3){
              echo"".$row['fname']." ".$row['sname'].", ";
              }
              }
          mysqli_close($dbConnection);
          }
?>

唯一的问题是,它显然也会为每个用户回显 "Position 2" 和 "Position 3"...关于如何拆分这两个位置的任何建议,以便同时循环发生在回声之后?我也试过像这样拆分 while 循环,但是 none 的名字出现了:

              $result = mysqli_query($ExampleDB,"SELECT * FROM members WHERE region=2 ORDER BY user DESC");

              echo "</br></br>Position 2: ";
              while($row2 = mysqli_fetch_array($result))
              {
                  if ($row2['position']==2){
                  echo"".$row2['fname']." ".$row2['sname']." ".$row2['user'].", ";
                  }
              }           
              echo "</br></br>Position 3: ";
              while($row3 = mysqli_fetch_array($result))
              {
                  if ($row3['position']==3){
                  echo"".$row['fname']." ".$row['sname'].", ";
                  }
              }

我也尝试过将它们全部保留为 $row,但也没有产生任何名称。如何拆分 while 循环?或者有没有更好的方法在位置 2,3 headers?

之后回显数组中的特定名称
$result = mysqli_query($ExampleDB,"SELECT * FROM members WHERE region=2 ORDER BY user DESC");

echo "</br></br>Position 2: ";
while($row2 = mysqli_fetch_array($result))
{
    if ($row2['position']==2)
    {
         echo"".$row2['fname']." ".$row2['sname']." ".$row2['user'].", ";
    }
}   

$result = mysqli_query($ExampleDB,"SELECT * FROM members WHERE region=2 ORDER BY user DESC");

echo "</br></br>Position 3: ";
while($row3 = mysqli_fetch_array($result))
{
     if ($row3['position']==3)
     {
        echo"".$row['fname']." ".$row['sname'].", ";
     }
}

如果您再次查询该语句,这实际上可以正常工作。在第一个 while 循环获取数组到达数组末尾之后。并没有执行获取数组 returns 。因此,您必须再次查询。

第一次输出header后可以设置flag。这并不理想,但可以避免重新设计您的方法:

$pos_two_header = true;
$pos_three_header = true;

while($row = mysqli_fetch_array($result))
    {
        if ($row['position']==2){
            if($pos_two_header) {
                echo "</br>Position 2: ";
                $pos_two_header = false;
             }
            echo"".$row['fname']." ".$row['sname'].", ";
        }
        if ($row['position']==3){
            if($pos_three_header) {
                echo "</br>Position 2: ";
                $pos_three_header = false;
             }
            echo"".$row['fname']." ".$row['sname'].", ";
        }
  }

根据 Jeroen 关于第二次使用 data_seek 而不是 运行 查询的评论,这将起作用,但不如简单地查找 0 索引那么容易。使用 PHP 5.4 处理此解决方案要容易得多,其中结果集作为 Traversable 返回。像这样:

$result = $ExampleDB->query("SELECT * FROM members WHERE region=2 ORDER BY user DESC");

foreach($result as $row)
    {
        echo "</br>Position 2: ";
        if ($row['position']==2){
            echo"".$row['fname']." ".$row['sname'].", ";
        }
    }

$result->field_seek(0);

foreach($result as $row)
    {
        echo "</br>Position 3: ";
        if ($row['position']==3){
            echo"".$row['fname']." ".$row['sname'].", ";
        }
    }