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

时间:2016-01-01 23:09:15

标签: php for-loop

我有一个包含SponsorName和HostName的SQL表。只有一个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

并不会遍历任何其他人。如果我取出代码来迭代主机,它可以正常工作并显示赞助商名称列表。这让我感到困惑,因为通过我的逻辑它应该有效,所以显然我的逻辑是有缺陷的!

我也知道我不应该将业务逻辑与设计相结合,但宝贝会在这里迈出一步! :d

2 个答案:

答案 0 :(得分:1)

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

不同

答案 1 :(得分:0)

&#13;
&#13;
 <?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
                }
            }                   
            ?>
&#13;
&#13;
&#13;