评论回复PHP脚本-只显示回复第一条评论 问题

评论回复PHP脚本-只显示回复第一条评论 问题,php,mysql,Php,Mysql,我正在创建一个标准的评论回复脚本,用户可以在文章中发表评论,也可以回复其他用户的评论我可以通过PHP和mySQL发布和检索评论问题是我无法检索所有评论的“回复”仅回复第一条评论。在我删除第一条评论后,会出现对第二条评论的回复 我的表的数据结构 如果一个人发表了新的评论,那么他的姓名和评论将以自动递增的id输入数据库;默认情况下,reply值为'0' 如果有人回复了一条评论,则所有详细信息都将以与上面相同的方式填写,除了回复值,该值取自回复发布的评论的id,如下所示 (mySQL表的结构如下所示)

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

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

问题是我无法检索所有评论的“回复”仅回复第一条评论。在我删除第一条评论后,会出现对第二条评论的回复

我的表的数据结构 如果一个人发表了新的评论,那么他的
姓名
评论
将以自动递增的
id
输入数据库;默认情况下,
reply
值为'0'

如果有人回复了一条评论,则所有详细信息都将以与上面相同的方式填写,除了
回复
值,该值取自回复发布的评论的
id
,如下所示 (mySQL表的结构如下所示):

我只在第一条评论下面看到回复。第二条评论的回复在我删除第一条评论后出现。
请帮忙!无法找出错误。

在第一个循环中查询答复。然而,我并不是说这是最好的解决方案

while($row=mysqli\u fetch\u数组($result)){
//在这里构造查询
$result\u reply=mysqli\u query($dbc,$query\u reply)
或die('错误查询数据库');
//……等等。
}

我认为你应该做什么:

    //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
//首先查询所有注释,即如果回复为0,则它是注释而不是回复
$result\u query\u statement=“从回复=0的注释中选择*”;
$result=mysqli_query($dbc,$result_query_语句)或die('Error querying database');
//循环注释获取数据并显示它
while($row=mysqli\u fetch\u数组($result)){
$id=$row['id'];
$name=$row['name'];
$comment=$row['comment'];
$reply=$row['reply'];
回声“
$name
$comment
";
//从数据库中获取并显示仍在该循环中的注释后,使用数据库中该注释的ID来查找对该特定注释的回复
//使用ID查询评论回复
//注意:由于ID($ID)不是0,我们只能在这里自动得到回复
$result\u reply\u query\u statement=“SELECT*FROM comments WHERE reply=$id”;
$result\u reply=mysqli\u query($dbc,$result\u reply\u query\u语句)或die('Error querying database');
//最后,oop浏览该评论的回复并显示回复
while($row=mysqli\u fetch\u数组($result\u reply)){
$name_reply=$row_reply['name'];
$comment_reply=$row_reply['comment'];
$id_reply=$row_reply['id'];
$reply_reply=$row_reply['reply'];
回声“
$name\u答复
";
回复:$comment\u回复
";
}
//关闭评论回复循环
}
//关闭注释循环
这是一种更容易使用的方法,不会引起混淆


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

这是一种会对数据库性能造成严重影响的解决方案。它对所有顶级帖子执行数据库查询,并对顶级帖子的每个直接子帖子执行附加查询。此外,此解决方案仅支持一个深度级别

下面是一个解决方案,它依赖于单个数据库查询和基于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);
}

?>

@Chuksy 您的代码的问题在于,它不会对第三级深度的回复进行回显。如果人们有几个层次,每个人都在不断地回复对方的回复,会怎么样?我们需要一个代码来匹配不确定的级别

下面是我如何编写代码的,但它只深入到两个层次,只捕获对每个原始注释的回复:


文件

使用递归,它是您的朋友:)
如果($reply==0)
将始终为真。我正在寻找相同的。非常感谢你。成功了!但它不会因为在每个注释后执行查询而减慢页面速度吗?
SELECT*FROM table WHERE reply=$parent
    //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
<?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);
}

?>