2017-09-13 76 views
-3

我在我的項目中創建一個評論系統。我希望每個發佈的問題都有評論。我能夠發表評論,但我有一個問題顯示所有評論,它是各自的答案。我能夠只顯示一行,但不顯示評論的行。我嘗試使用嵌套在回聲的每個問題中的while循環,但它會掛起。當我使用時,如果它只顯示每個問題的評論的第一行。我可以使用while循環內另一個while循環在php

所以我的問題是如何顯示它們呢?

<div class="answer"> 
    <?php 
    include 'db.php'; 
     $sql = "select * from answers where questionrid IN(select id from question where id='$qid')"; 
     $result = mysqli_query($con,$sql); 
     if($result){ 
    while($ro = mysqli_fetch_assoc($result)){ 
    ?> 

    <div class="a_view"> 
    <pre> 
    <?php 

     echo $ro["answer"]; 
     ?> 
    </pre> 
     </div> 

     <div class="ans_comment"> 
      <?php 
      if($ro["id"]){ 
      $id = $ro["id"]; 
      $sqli = "SELECT * FROM comments WHERE answerid='$id'"; 
      $query = mysqli_query($con,$sqli); 
     $row = mysqli_fetch_assoc($query); 
      $num = mysqli_num_rows($query); 
      while($row){ 
      ?> 
      <div><?php echo $row["comments"];?></div> 
      <?php 

      } 
      } 
      ?> 
     </div> 
     <div class="add"><div class="coment">add a comment</div> <div id="coment"> 
     <form class="cform" method="post" action="acomment.php"> 
     <textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is 
     required to give correction or more information about the problem"></textarea><br><br> 
     <input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid"> 
     <input type="submit" value="Post your comment"> 
    </form> 
    </div></div> 
     <?php 
     } 
     }else{ 
      echo "no record"; 
     } 
     ?> 

     <?php 
     $con->close(); 
     ?> 

這是由它掛

while($row){ 
      ?> 
      <div><?php echo $row["comments"];?></div> 
      <?php 

      } 
當我使用 if

,它只是回聲一行的部分。

+2

所有我看到的是'php'代碼,其中沒有一個是'JavaScript'! – Deano

+2

答案,在JavaScript中,或在PHP中,這是你實際顯示,是YES。 確保每個while()循環都有一個轉義的方法(這通常是一個計數器,用於增加while()正在測試的變量)。還要確保(看起來你已經完成了)在編寫內部循環時不會覆蓋外部循環中的變量名稱。 –

+3

我沒有看到第二個'while'循環,到目前爲止您嘗試了什麼? –

回答

2

而不是

while($row){ 

做到這一點,就像你在while循環正在做上述

while($row = mysqli_fetch_assoc($query)){ 

你現在的樣子,$row是永遠不會改變的,因此始終計算爲true,讓你陷入你的循環。