我有一个评论框,用户可以发表评论。这些注释将在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>";
}
?>
现在其他用户也可以回复用户的评论。我想在其他用户的每条评论下面显示他们的回复,但我似乎无法弄清楚如何做到这一点。
我应该在第一个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 :(得分: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)){
}
}
?>
答案 1 :(得分: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>";
}
?>