评论 - 回复PHP脚本 - 仅回复第一条评论

时间:2015-04-16 22:05:39

标签: php mysql

问题

我正在创建一个标准的评论回复脚本,用户可以在文章中发表评论,也可以回复其他用户的评论。

我可以通过PHP和mySQL发布和检索评论。

问题是我无法找回回复'所有评论。 仅针对第一条评论的回复 。删除第一条评论后,会显示对第二条评论的回复。

我桌子的数据结构

如果某人发布了新评论,那么他的namecomment将通过自动递增id输入到数据库中;默认情况下,reply值为' 0'

如果有人回复评论,那么所有详细信息都将以与上述相同的方式填写,但reply值除了评论的id值,其评论的回复发布如下所示 (mySQL表的结构看起来像这样):

|id | name  |            comment            | reply |
|---|-------|-------------------------------|-------|
| 1 |Pranav | This is Pranav's Comment      |   0   | 
| 2 |Anita  | This is the Anita's Comment   |   0   |
| 3 |Rishab | 1st reply to Pranav's comment |   1   |
| 4 |Paul   | 1st reply to Anita's comment  |   2   |
| 5 |James  | 2nd reply to Pranav's comment |   1   |
| 6 |Rachel | This is Rachel's comment      |   0   |
| 7 |Pranshu| 1st Reply to Rachel's comment |   6   |
| 8 |Leora  | 2nd Reply to Anita's comment  |   2   |
| 9 |Pekka  | 3rd Reply to Anita's comment  |   2   |

例如。如果有人回复Anita的评论id = 2;然后,发布的新回复的reply列的值将自动变为2.

PHP代码

$result按住评论数组
$result_reply持有对这些意见的答复 while($row = mysqli_fetch_array($result))循环显示评论
编写嵌套的while($row_reply = mysqli_fetch_array($result_reply))循环以显示每条评论下的相应回复,但这并没有发生。
if($reply_reply==$id)循环检查reply column code的{​​{1}}特定注释,以便特定回复仅显示在相应注释下。

数据库的名称是' oxygentimes'和表是'评论'

id

我只看到第一条评论下的回复。删除第一条评论后,会显示第二条评论的回复。 请帮忙!无法弄清楚错误。

4 个答案:

答案 0 :(得分:3)

查询第一个循环内的回复。我并不是说它是最好的解决方案。

while ($row = mysqli_fetch_array($result)) {

    // construct query here

    $result_reply = mysqli_query($dbc, $query_reply)
        or die('Error quering database');
    //... etc.
}

答案 1 :(得分:2)

我认为你应该做的事情:

    //Query for all comments first i.e. where reply is 0 it is a comment not a reply
    $result_query_statement = "SELECT * FROM comments WHERE reply = 0";
    $result = mysqli_query($dbc, $result_query_statement) or die('Error quering database');

    //Loop through comments get the data and display it
    while ($row = mysqli_fetch_array($result)) {
    $id = $row['id'];
    $name = $row['name'];
    $comment = $row['comment'];
    $reply = $row['reply'];
    echo "
    <div class='comment'>
        <div>$name</div>
        <div>$comment</div>
        <input type='hidden' value='$id'>
    </div>";

    //After getting and displaying a comment from the database still in that loop use the ID of that comment from the database to look for the replies to that particular comment

    //Using the ID to query for comment replies
    //Note: Since the ID($id) is not 0 we would only get replies here automatically
    $result_reply_query_statement = "SELECT * FROM comments WHERE reply = $id";
    $result_reply = mysqli_query($dbc, $result_reply_query_statement) or die('Error quering database');

    //Finally oop through the replies gotten for that comment and display the reply
    while ($row = mysqli_fetch_array($result_reply)) {
        $name_reply = $row_reply['name'];
        $comment_reply = $row_reply['comment'];
        $id_reply = $row_reply['id'];
        $reply_reply = $row_reply['reply'];

            echo "
            <div class='reply-comment'>
                <div>$name_reply</div>
                <input type='hidden' value='$id_reply'>";
            echo "<div>Reply: $comment_reply</div>
            </div>";
    }
    //Closes the Comments Reply Loop
    }
    //Closes the Comments Loop

这是一种更容易使用的方法,不会让人感到困惑。

如果我犯了某种错误,请纠正我。

答案 2 :(得分:2)

这是一种会导致数据库性能受损的解决方案。它为所有顶级帖子执行数据库查询,并为顶级的每个直接子项执行附加查询。此解决方案也只支持一个深度。

下面是一个依赖于单个数据库查询和基于PHP的循环中的2n次迭代的解决方案。它还允许无限深度的评论树(评论评论)。这只是一个概念性的解决方案,因此我使用了虚拟数据和CLI。

<?php

$comments = array(
  array(
    'id' => 1,
    'parent_id' => 0
  ),
  array(
    'id' => 2,
    'parent_id' => 0
  ),
  array(
    'id' => 3,
    'parent_id' => 1
  ),
  array(
    'id' => 4,
    'parent_id' => 2
  ),
  array(
    'id' => 5,
    'parent_id' => 2
  ),
  array(
    'id' => 6,
    'parent_id' => 4,
  ),
  array(
    'id' => 7,
    'parent_id' => 0
  )
);

$grouped = array();
foreach ($comments as $comment) {
  if (!isset($grouped[$comment['parent_id']])) {
    $grouped[$comment['parent_id']] = array();
  }
  $grouped[$comment['parent_id']] []= $comment;
}

function render_comment($comment, &$grouped, $depth = 0) {
  for ($i=0; $i<$depth; $i++) {
    echo "\t";
  }
  echo $comment['id'];
  echo "\n";
  foreach ($grouped[$comment['id']] as $reply) {
    render_comment($reply, $grouped, $depth+1);
  }
}

foreach ($grouped[0] as $top_level_comment) {
  render_comment($top_level_comment, $grouped);
}

?>

答案 3 :(得分:1)

@Chuksy 您的代码的问题在于,它不会回显对答复的答复,而答复将是第3层。如果人们有多个层次,每个人不断回答对方的回复,该怎么办?我们需要一个代码来匹配不确定的级别。

这是我重新编写您的代码的方式,但是它只深入了2个级别,仅捕获了每个原始评论的回复:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
// Create connection
$conn = new mysqli('localhost', 'root', 'Jordan123', 'commentsystem2');

$sql1 = "SELECT * FROM comments WHERE reply_id = 0";
$result1 = mysqli_query($conn, $sql1);

while ($comment = mysqli_fetch_array($result1)) {
    $id = $comment['id'];
    $name = $comment['name'];
    $comment = $comment['comment'];
    
echo '
<div class="comments" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;">
<div>'.$name.'</div>
<div>'.$comment.'<br><br></div>
</div>
';

$sql2 = "SELECT * FROM comments WHERE reply_id = $id";
$result2 = mysqli_query($conn, $sql2);
while ($reply = mysqli_fetch_array($result2)) {
$id_reply = $reply['id'];
$reply_name = $reply['name'];
$reply_comment = $reply['comment'];
$reply_id = $reply['reply_id'];


echo '
<div class="replies" style="position:relative; margin:auto; width:500px; border:1px solid black; margin-top:1px;">
<div style="width:80%; text-align:center;">'.$reply_name.' replied to '.$name.'</div>
<div style="width:80%; text-align:center;">'.$reply_comment.'<br><br></div>
</div>
';

 }//end of replies while loop

}//end of comments while loop

?>
</body>
</html>