0

我有一个评论框,用户可以在其中发表评论。这些注释将在一个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
}
4

2 回答 2

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)){

   }

}

?>
于 2013-09-21T07:50:33.073 回答
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>";
}

?>
于 2013-09-21T07:53:41.343 回答