PHP for循环内部的for循环只产生第一个结果


PHP for loop inside for loop only producing first result

我有一个SQL表,其中包含SponsorName和HostName。只有一个HostName,但一个HostName可以共享相同的SponsorName。例如:

id    SponsorID    hostID    SponsorName    HostName
1     36           255       John           Jack
2     31           221       Bob            Paul
3     36           452       John           Terry
4     32           245       Trevor         Peter

我想返回一份所有赞助商"东道主"的名单。为了做到这一点,我创建了以下代码:(介绍PHP编码,所以它可能是假的!)

                <?php
                $query = "SELECT sponsorID, SponsorName FROM gaming_sponsors GROUP BY SponsorName ORDER BY SponsorName ASC";
                $result = mysqli_query($mysqli, $query);    
                @$num_results = mysqli_num_rows($result);
                for($i=0; $i<$num_results; $i++) {  
                    $row = mysqli_fetch_assoc($result);
            ?>
                    <tr>
                        <td colspan="2" align="center" class="schedHeaderTime"><?php echo $row['SponsorName']; ?>'s Hosts</td>
                    </tr>
                    <?php
                    $query4 = "SELECT hostID, SponsorHostName FROM gaming_sponsors WHERE sponsorID = " . $row['sponsorID'] . " ORDER BY SponsorHostName ASC";
                    $result4 = mysqli_query($mysqli, $query4);  
                    @$num_results4 = mysqli_num_rows($result4);
                    for($i=0; $i<$num_results4; $i++) { 
                        $row4 = mysqli_fetch_assoc($result4);   
                    ?>
                        <tr>
                            <td width="90%" align="center"><?php echo $row4['SponsorHostName']; ?></td> 
                            <td width="10%" align="center"><a href="adm_sponsor_update.php?id=<?php echo $row4['hostID']; ?>">View / Edit</a></td>
                        </tr> 
            <?php
                    }
                }                   
            ?>

我希望它能退回这个:

John's Hosts
Jack
Terry
Bob's Hosts
Paul
Tervor's Hosts
Peter

但是,它只返回

John's Hosts
Jack
Terry

并且不会遍历其他任何一个。如果我取出代码来遍历Host,它会很好地工作,并显示赞助商名称列表。这让我很困惑,因为通过我的逻辑,它应该是有效的,所以很明显我的逻辑是有缺陷的!

我也知道我不应该把商业逻辑和设计结合起来,但要循序渐进!:D

在两个循环中,都使用$i作为表达式中的变量。在第二个循环中,将其更改为$j或其他,与第一个循环不同,将其命名为

 <?php
 $query = "SELECT SponsorName FROM gaming_sponsors ORDER BY SponsorName ASC"; //Select all SponsorName. 
//May be repeated too like in the case of John
$result = mysqli_query($mysqli, $query);    
$tempArray = [];
if( mysqli_num_rows($result ) > 0)
  {
    while ($row = mysqli_fetch_array($result1)) { //For all such Sponsors, 
//get their Hostsname
      
      if(!in_array($row['SponsorName'] , $tempArray)) // Check if a Sponsor is 
//already handled here for the first time when John comes, this 'if' gets executed 
//but next time when John comes, it will neglect the if and will prevent repetition.
        {
            ?>
                    <tr>
                        <td colspan="2" align="center" class="schedHeaderTime"><?php echo $row['SponsorName']; ?>'s Hosts</td>
                    </tr>
                    <?php
                    $query4 = "SELECT hostID, SponsorHostName FROM gaming_sponsors WHERE SponsorName  = " . $row['SponsorName'] . " "; //Get all hostnames for this sponsor
                    $result4 = mysqli_query($mysqli, $query4);  
                    while ($row4 = mysqli_fetch_array($result4)) {
                    ?>
                        <tr>
                            <td width="90%" align="center"><?php echo $row4['SponsorHostName']; ?></td> 
                            <td width="10%" align="center"><a href="adm_sponsor_update.php?id=<?php echo $row4['hostID']; ?>">View / Edit</a></td>
                        </tr> 
            <?php
                    }
                }
                array_push($tempArray, $row['SponsorName']); //John is added in this array, so that next time John comes, we skip the 'if' statement
                }
            }                   
            ?>