Nidified mysqli_fetch_assoc while

时间:2015-04-28 08:08:03

标签: php html

我遇到以下代码的问题:

$query = "SELECT * FROM movie_list WHERE id=$id";
 $result_q = mysqli_query($conn, $query);
 $row = mysqli_fetch_assoc($result_q);

 if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
    $query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
    $result_q_serie = mysqli_query($conn, $query_serie);


    $query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
    $result_q_serie_cont = mysqli_query($conn, $query_serie_cont);


[...]


<?php
  while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
    <li>
      <a class="collapsible-header collapsible-header  waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
        <div class="collapsible-body">
          <ul>
          <?php
            while($row_serie = mysqli_fetch_assoc($result_q_serie)){ 
              if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
                echo "<li><a href=player.php?id=".$row_serie['id'].">".$row_serie['episode_number']."</a></li>";
              }
            }
            echo "</ul>";
            echo "</div>";
  }
?>
   </li>

第一次完成时效果很好,但第二次缺少while($ row_serie)变量。调试页面我看到$ row_serie变量在完成所有第一次while($ row_serie_cont)后消失但在第二次触发时根本没有重新出现。

我在代码中遗漏了什么?

3 个答案:

答案 0 :(得分:0)

在循环播放后关闭li

while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
    <li>
      <a class="collapsible-header collapsible-header  waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
        <div class="collapsible-body">
          <ul>
          <?php
            while($row_serie = mysqli_fetch_assoc($result_q_serie)){ 
              if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
                echo "<li><a href=player.php?id=".$row_serie['id'].">".$row_serie['episode_number']."</a></li>";
              }
            }
            echo "</ul>";
            echo "</div>";
 echo "</li>"// inside the loop
  }
?>

答案 1 :(得分:0)

似乎在执行操作时将$result_q_serie = mysqli_query($conn, $query_serie);插入第二个。

我不明白为什么我需要重新填充result_q_serie,但现在它可以了。

如果有人可以提出更好的解决方案,我会听到

<?php
            while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
    <li class="no-padding">
    <ul class="collapsible collapsible-accordion">
        <li>
        <a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
        </a>
        <div class="collapsible-body">
            <ul>
                <?php
                                    $result_q_serie = mysqli_query($conn, $query_serie);
                                    while($row_serie = mysqli_fetch_assoc($result_q_serie)){ 
                                        if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
                                            echo "<li>
                <a href=player.php?id=".$row_serie['id'].">".$row_serie['episode_number']."</a></li>
                "; } } echo "
            </ul>
            "; echo "
        </div>
                </li>


    </ul>
    </li>
        "; } ?>
        </ul>

答案 2 :(得分:0)

似乎使用 mysqli_data_seek($ result,0); 以正确的方式进行操作。