2016-09-14 156 views
1

Db db2 代碼示例到目前爲止只列出所有帖子,但只顯示一條評論? 代碼我目前只顯示一條評論?PHP while while語句中的語句?

請任何人的協助我真的需要幫助是在編碼noob,並盡我所能。謝謝你在前進

<?php 
include("../db/db.php"); 
{ 

    /*if(isset($_POST["TimeLine"])){*/ 
    $queryposts = "SELECT * FROM post"; 
    $run_query = mysqli_query($con,$queryposts); 
    while($row = mysqli_fetch_array($run_query)){ 
     $post_id = $row["post_id"]; 
     $uid = $row["user_id"]; 
     $content = $row["content"]; 
     $like = $row["total_like"]; 
     $date = $row["date_created"]; 
     $uidpic = $row["profilepic"]; 
     $email = $row["email"]; 
     $pImage = $row["postimage"]; 
     $pVideo = $row["video"]; 
     echo "<b>POST: <b/><hr />". 
      $content."<hr >" 
     ; 


     $querycomment = "SELECT * FROM comment WHERE comment_id = $post_id"; 
     $run_queryb = mysqli_query($con,$querycomment); 
     while($row = mysqli_fetch_array($run_queryb)){ 
      $ComId = $row["comment_id"]; 
      $Comment = $row["comment"]; 
      $ComDate = $row["comment_date"]; 
      $ProfilePicpost = $row["profilepic"]; 
      echo "<b>Comment: <b/><hr />". 
       $Comment."<hr >" 
      ; 
     } 
    } 
} 

enter image description here

+0

您正在重置每個while循環中的'$ row'。對帖子和評論使用不同的行變量。 – aynber

回答

0

您在內環覆蓋相同的變量$row。將其名稱更改爲其他任何內容。

$queryposts = "SELECT * FROM post"; 
$run_query = mysqli_query($con,$queryposts); 

while($posts = mysqli_fetch_array($run_query)){ 

    // Your post related code 
    $post_id = $posts["post_id"]; 
    $uid = $posts["user_id"]; 
    // Use $posts instead of $row 

    $querycomment = "SELECT * FROM comment WHERE comment_id = $post_id"; 
    $run_queryb = mysqli_query($con,$querycomment); 

    while($comments = mysqli_fetch_array($run_queryb)){ 

     // Your comment related code 
     $ComId = $comments["comment_id"]; 
     $Comment = $comments["comment"]; 
     // Use $comments instead of $row 

    } 
} 
+0

仍然相同的結果$評論只顯示一個結果,並不循環到一個單一職位的下一個評論? – RaydenPe

+0

讓我看看你的數據庫中的數據。您可以在您的問題中添加它的屏幕截圖,或者添加一個鏈接到sqlfiddle。 –

+0

https://www.dropbox.com/sh/bress07ynezkt5p/AADmbwE8ZE-5M3YWYa4wo_-Ca?dl=0 – RaydenPe