这个问题在这里已经有了答案:
如何在不同的页面上获得不同的评论(1个答案)
昨天关门了。
我有一个php博客,我正在尝试添加一个评论系统。我可以让评论系统工作,但是评论会出现在博客的每一篇文章上。
我应该在哪里插入代码,使每个帖子都有其独特的评论,而不是在每个帖子上显示相同的评论?我已经试过了,但没有做对。
以下是我尊敬的代码:
comment.php
<h2><i>Drop a comment here</i></h2>
<div class="comment-form-container">
<form id="frm-comment">
<div class="input-row">
<input type="hidden" name="post_id" id="post_id" value="<?php echo $postID_id; ?>" />
<input type="hidden" name="comment_id" id="commentId"
placeholder="Name" /> <input class="input-field"
type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="input-row">
<textarea class="input-field" type="text" name="comment"
id="comment" placeholder="Add a Comment" required > </textarea>
</div>
<div>
<input type="button" class="btn-submit" id="submitButton"
value="Publish" /><div id="comment-message">Comments Added Successfully!</div>
</div>
</form>
</div>
<div id="output"></div>
<script>
function postReply(commentId) {
$('#commentId').val(commentId);
$("#name").focus();
}
$("#submitButton").click(function () {
$("#comment-message").css('display', 'none');
var str = $("#frm-comment").serialize();
$.ajax({
url: "../comments/comment-add.php",
data: str,
data: {post_id : <?php echo $postID; ?>},
type: 'post',
success: function (response)
{
var result = eval('(' + response + ')');
if (response)
{
$("#comment-message").css('display', 'inline-block');
$("#name").val("");
$("#comment").val("");
$("#commentId").val("");
$("#post_id").val("");
listComment();
} else
{
alert("Failed to add comments !");
return false;
}
}
});
});
$(document).ready(function () {
listComment();
});
function listComment() {
$.post("../comments/comment-list.php",
function (data) {
var data = JSON.parse(data);
var comments = "";
var replies = "";
var item = "";
var parent = -1;
var results = new Array();
var list = $("<ul class='outer-comment'>");
var item = $("<li>").html(comments);
for (var i = 0; (i < data.length); i++)
{
var commentId = data[i]['comment_id'];
parent = data[i]['parent_comment_id'];
if (parent == "0")
{
comments = "<div class='comment-row'>"+
"<div class='comment-info'><span class='commet-row-label'>from</span> <span class='posted-by'>" + data[i]['comment_sender_name'] + " </span> <span class='commet-row-label'>at</span> <span class='posted-at'>" + data[i]['date'] + "</span></div>" +
"<div class='comment-text'>" + data[i]['comment'] + "</div>"+
"<div><a class='btn-reply' onClick='postReply(" + commentId + ")'>Reply</a></div>"+
"</div>";
var item = $("<li>").html(comments);
list.append(item);
var reply_list = $('<ul>');
item.append(reply_list);
listReplies(commentId, data, reply_list);
}
}
$("#output").html(list);
});
}
function listReplies(commentId, data, list) {
for (var i = 0; (i < data.length); i++)
{
if (commentId == data[i].parent_comment_id)
{
var comments = "<div class='comment-row'>"+
" <div class='comment-info'><span class='commet-row-label'>from</span> <span class='posted-by'>" + data[i]['comment_sender_name'] + " </span> <span class='commet-row-label'>at</span> <span class='posted-at'>" + data[i]['date'] + "</span></div>" +
"<div class='comment-text'>" + data[i]['comment'] + "</div>"+
"<div><a class='btn-reply' onClick='postReply(" + data[i]['comment_id'] + ")'>Reply</a></div>"+
"</div>";
var item = $("<li>").html(comments);
var reply_list = $('<ul>');
list.append(item);
item.append(reply_list);
listReplies(data[i].comment_id, data, reply_list);
}
}
}
</script>
comment-add.php
<?php
require_once ("db.php");
$commentId = isset($_POST['comment_id']) ? $_POST['comment_id'] : "";
$post_id = isset($_POST['post_id']) ? $_POST['post_id'] : "";
$comment = isset($_POST['comment']) ? $_POST['comment'] : "";
$commentSenderName = isset($_POST['name']) ? $_POST['name'] : "";
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO tbl_comment(parent_comment_id,post_id,comment,comment_sender_name,date) VALUES ('" . $commentId . "','" . $post_id . "','" . $comment . "','" . $commentSenderName . "','" . $date . "')";
$result = mysqli_query($conn, $sql);
if (! $result) {
$result = mysqli_error($conn);
}
echo $result;
?>
comment-list.php
<?php
require_once ("db.php");
$qry2=mysqli_query($con,"select * from sa_posts where postID ='".$_GET['postID']."'");
$post=mysqli_fetch_array($qry2);
$sql = "SELECT * FROM tbl_comment ORDER BY parent_comment_id asc, comment_id asc";
$result = mysqli_query($conn, $sql);
$record_set = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($record_set, $row);
}
mysqli_free_result($result);
mysqli_close($conn);
echo json_encode($record_set);
?>
2条答案
按热度按时间irtuqstp1#
在comment-list.php中,在comments查询中添加post id。
从tbl_注解中选择*,其中post_id=“$_获取['postid']按父项注解id排序,注解id asc
zf9nrax12#
在comment-list.php中,您应该在查询中指定post_id。
因此改变
到
另一方面,函数listcoment()调用comment-list.php
但似乎没有传递给此函数的参数来指定post_id
因此,您应该将listcomment()修改为listcomment(post_id),然后将post_id作为say post或get参数传递给comment-list.php。