2013-09-21 185 views
0

我有一個評論框,其中用戶可以發表評論。這些註釋將在while循環中顯示。while While Loop While While Loop

<?php 

while ($rowsx=mysql_fetch_array($resultss1)){ 

echo "<div id='comments'>"; 
echo "<table class='table11'>"; 
echo "<tr>"; 
echo "<td class='sss'><img src='img/user.jpg'>"; 
echo "<p class='p999'>" .$rowsx['username']; 
echo "</p> </br>"; 
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> "; 
echo "</td>"; 
echo "<td>"; 
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>"; 


echo "</td>"; 

echo "</tr>"; 
echo "</table>"; 

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."' class='reply_button'> REPLY </a> "; 
echo "</div>"; 

} 

?> 

現在其他用戶也可以回覆用戶的評論。我想在其他用戶的每條評論下面顯示他們的回覆,但我似乎無法弄清楚如何做到這一點。

我應該在第一個while循環內使用另一個while循環嗎?

對不起,如果我使用的是mysql函數,我知道這是貶值,但它只是一個學校項目。

這裏是我的顯示留言查詢:

$display_comments1="Select username,prod_id,comment,DATE_FORMAT(date_posted, '%m/%d/%Y %H:%i:%s') AS date_posted from comment where prod_id='$prod_id' order by date_posted DESC"; 
$resultss1=mysql_query($display_comments1); 

if($resultss1 === FALSE) { 
die(mysql_error()); // TODO: better error handling 
} 
+0

聽起來像[遞歸]的工作(http://en.wikipedia.org/wiki/Recursion#Recursion_in_computer_science) – Rob

+1

是在while循環內放置了一個while循環 –

+0

不知道數據庫的結構,很難猜測註釋之間的相互關聯程度。 – raina77ow

回答

6

是的,你可以把while循環while循環看起來像這樣:

<?php 

while ($rowsx=mysql_fetch_array($resultss1)){ 

echo "<div id='comments'>"; 
echo "<table class='table11'>"; 
echo "<tr>"; 
echo "<td class='sss'><img src='img/user.jpg'>"; 
echo "<p class='p999'>" .$rowsx['username']; 
echo "</p> </br>"; 
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> "; 
echo "</td>"; 
echo "<td>"; 
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>"; 


echo "</td>"; 

echo "</tr>"; 
echo "</table>"; 

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."' class='reply_button'> REPLY </a> "; 
echo "</div>"; 

    $commentId = $rowsx['id']; 
    $replies = mysql_query('SELECT * FROM replies WHERE comment_id = $commentId'); 

    while ($rowsx=mysql_fetch_array($replies)){ 

    } 

} 

?> 
+0

謝謝您的回覆先生...我會試試這個,如果這個作品回來 – Bongsky

+0

這使得只有一級rply嘗試遞歸 –

+0

這個作品...謝謝!!! – Bongsky

1

創建這種類型的代碼:

<?php 

while ($rowsx=mysql_fetch_array($resultss1)){ 

    print_comment($rowsx) 

} 

function print_comment($rowsx) { 
    echo "<div id='comments'>"; 
    echo "<table class='table11'>"; 
    echo "<tr>"; 
    echo "<td class='sss'><img src='img/user.jpg'>"; 
    echo "<p class='p999'>" .$rowsx['username']; 
    echo "</p> </br>"; 
    echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> "; 
    echo "</td>"; 
    echo "<td>"; 
    echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>"; 


    echo "</td>"; 

    echo "</tr>"; 
    echo "</table>"; 

    echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."' class='reply_button'> REPLY </a> "; 

    while ($sub_comment_result=mysql_fetch_array($reply)){ 

     print_comment($sub_comment_result) 

    } 
    echo "</div>"; 
} 

?>