我正在创建一个标准的评论-答复脚本,用户可以在其中发表文章中的评论,也可以回复其他用户的评论。我能够通过PHP和mySQL发布和检索评论。问题是我无法检索所有评论的“回复”。 仅出现第一个评论的回复。我删除第一条评论后,出现第二条评论的回复。
[如果某人发布新评论,则其name
和comment
会以自动递增的id
输入到数据库中;默认情况下,reply
值为'0'。如果有人回复评论,则所有细节均以与上述相同的方式填充,除了reply
值取自发表了回复的评论的id
中的值,如下所示(mySQL表的结构看起来像这样):
| id |名称|评论|回复|| --- | ------- | ------------------------------- | ----- -|| 1 | Pranav |这是Pranav的评论| 0 || 2 |阿妮塔|这是安妮塔的评论| 0 || 3 |利沙卜|对Pranav的评论的第一回复| 1 || 4 |保罗|对Anita的评论的第一回复| 2 || 5 |詹姆斯|对Pranav的评论的第二个回复| 1 || 6 |瑞秋|这是瑞秋的评论| 0 || 7 | Pranshu | 1回复Rachel的评论| 6 || 8 | Leora |第二次回复安妮塔的评论| 2 || 9 | Pekka |第三回复Anita的评论| 2 |
例如如果有人回复Anita的id = 2
评论;然后,新发布的回复的reply
列的值将自动变为2。
$result
保留注释数组$result_reply
保留这些评论的回复while($row = mysqli_fetch_array($result))
循环显示注释编写了嵌套的while($row_reply = mysqli_fetch_array($result_reply))
循环,以在每个注释下显示相应的答复,但这不会发生。if($reply_reply==$id)
循环将特定评论的reply column code
与id
进行比较,以便仅在相应评论下显示特定答复。数据库的名称为“ oxygentimes”,表的名称为“ comments”]
<?php
$dbc = mysqli_connect('localhost', 'root', '', 'oxygentimes') or die('Error connecting to mysql server');
$query = "SELECT * FROM `comments` WHERE reply=0";
$query_reply = "SELECT * FROM `comments` WHERE reply!=0";
$result = mysqli_query($dbc, $query) or die('Error quering database');
$result_reply = mysqli_query($dbc, $query_reply) or die('Error quering database');
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$comment = $row['comment'];
$reply = $row['reply'];
if ($reply == 0) {
echo "
<div class='comment'>
<div>$name</div>
<div>$comment</div>
<input type='hidden' value='$id'>
</div>";
while ($row_reply = 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'];
if ($reply_reply == $id) {
echo "
<div class='reply-comment'>
<div>$name_reply</div>
<input type='hidden' value='$id_reply'>";
echo "<div>Reply: $comment_reply</div>
</div>";
};
};
}
}
我仅在第一条评论下看到回复。我删除第一条评论后,出现第二条评论的回复。请帮忙!无法找出错误。
查询第一个循环内的答复。我并不是说这是最好的解决方案。
while ($row = mysqli_fetch_array($result)) {
// construct query here
$result_reply = mysqli_query($dbc, $query_reply)
or die('Error quering database');
//... etc.
}
我认为您应该做什么:
//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的循环中的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您的代码的问题在于,它不会回显对答复的答复,而答复将是第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>