问题描述
我想通过使用帖子 ID 为多个帖子创建一个动态评论系统。但我无法使用帖子 ID 获取所有帖子的评论。通过此代码所有评论都显示在所有帖子中。如何通过 id 正确获取特定帖子的评论。
Post.PHP
<?PHP include 'inc/header.PHP'; ?>
<?PHP
if (!isset($_GET['id']) || $_GET['id'] == NULL) {
header("Location: 404.PHP");
} else {
$id = $_GET['id'];
}
?>
<div class="container contentsection contemplete">
<div class="row">
<div class="col-md-9">
<div class="maincontent">
<div class="about">
<div class="post-<?PHP echo $id; ?>">
<?PHP
$query = "SELECT * FROM tbl_post WHERE id=$id";
$post = $db->select($query);
if ($post) {
while ($result = $post->fetch_assoc()) {
?>
<h2><?PHP echo $result['title']; ?></h2>
<h4><?PHP echo $fm->formatDate($result['postdate']); ?>,By <a href="#"><?PHP echo $result['author']; ?></a></h4>
<img src="admin/upload/<?PHP echo $result['image']; ?>" alt="post image"/>
<?PHP echo $result['body']; ?>
<?PHP } ?>
<div class="c-Box comment-section" style="border-top: 3px dotted #5f7fa7">
<div class="heade" >
<form method="POST" id="comment_form">
<div class="form-group">
<input type="text" hidden name="comment_name" id="comment_name" value="<?PHP echo Session::get('username') ?>" class="form-control" />
</div>
<div class="form-group">
<textarea style="width: 100%; background: #FFF; border: 1px solid #370aa545; font-size: 17px;" name="comment_content" id="comment_content" class="form-control" placeholder="Enter Comment" rows="5"></textarea>
</div>
<div class="form-group">
<input hidden name="post_id" id="post_id" class="form-control" value="<?PHP echo $id; ?>" />
<input type="hidden" name="comment_id" id="comment_id" value="0" />
<input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
<span id="comment_message"></span>
<div id="<?PHP echo"id-"; ?><?PHP echo $id; ?>" class="inside">
<h2>COMMENTS:</h2>
<div id="display_comment"></div>
</div>
</div>
<script>
$(document).ready(function(){
$('#comment_form').on('submit',function(event){
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url:"add_comment.PHP",method:"POST",data:form_data,dataType:"JSON",success:function(data)
{
if(data.error != '')
{
$('#comment_form')[0].reset();
$('#comment_message').html(data.error);
$('#comment_id').val('0');
load_comment();
}
}
})
});
load_comment();
function load_comment()
{
$.ajax({
url:"fetch_comment.PHP",success:function(data)
{
$('#display_comment').html(data);
}
})
}
$(document).on('click','.reply',function(){
var comment_id = $(this).attr("id");
$('#comment_id').val(comment_id);
$('#comment_content').focus();
});
});
</script>
<?PHP
} else { header("Location:404.PHP");}
?>
</div>
</div>
</div>
</div>
<?PHP include 'inc/sidebar.PHP'; ?>
</div> <!-- Row -->
</div> <!-- Conrainer-fluid -->
<?PHP include 'inc/footer.PHP'; ?>
add_comment.PHP
<?PHP
$connect = new PDO('MysqL:host=localhost;dbname=data_range','root','');
$error = '';
$comment_name = '';
$post_id = '';
$comment_content = '';
if(empty($_POST["post_id"]))
{
$error .= '<p class="text-danger">ID is required</p>';
}
else
{
$post_id = $_POST["post_id"];
}
if(empty($_POST["comment_name"]))
{
$error .= '<p class="text-danger">Name is required</p>';
}
else
{
$comment_name = $_POST["comment_name"];
}
if(empty($_POST["comment_content"]))
{
$error .= '
<script type="text/javascript">
swal({
title: "Error!",text: "Comment Is Requered!",icon: "error",button: "OK",});
</script>
';
}
else
{
$comment_content = $_POST["comment_content"];
}
if($error == '')
{
$query = "
INSERT INTO tbl_comment
(parent_comment_id,post_id,comment,comment_sender_name)
VALUES (:parent_comment_id,:post_id,:comment,:comment_sender_name)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':parent_comment_id' => $_POST["comment_id"],':comment' => $comment_content,':post_id' => $post_id,':comment_sender_name' => $comment_name
)
);
$error = '
<script type="text/javascript">
swal({
title: "Congratulations!",text: "Comment Successfully Added!",icon: "success",});
</script>
';
}
$data = array(
'error' => $error
);
echo json_encode($data);
?>
fetch_comment.PHP
<?PHP
$connect = new PDO('MysqL:host=localhost;dbname=data_range','');
$query = "
SELECT * FROM tbl_comment
WHERE parent_comment_id = '0'
ORDER BY comment_id DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
$output .= '
<div class="panel panel-default">
<div class="panel-heading"><h3 style="font-size: 19px;">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></h3></div>
<div class="panel-body"><p style="font-size: 17px;">'.$row["comment"].'</p></div>
<div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
</div>
';
$output .= get_reply_comment($connect,$row["comment_id"]);
}
echo $output;
function get_reply_comment($connect,$parent_id = 0,$marginleft = 0)
{
$query = "
SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
";
$output = '';
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$count = $statement->rowCount();
if($parent_id == 0)
{
$marginleft = 0;
}
else
{
$marginleft = $marginleft + 48;
}
if($count > 0)
{
foreach($result as $row)
{
$output .= '
<div class="panel panel-default" style="margin-left:'.$marginleft.'px">
<div class="panel-heading"><h3 style="font-size: 19px;">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></h3></div>
<div class="panel-body"><p style="font-size: 17px;">'.$row["comment"].'</p></div>
<div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
</div>
';
$output .= get_reply_comment($connect,$row["comment_id"],$marginleft);
}
}
return $output;
}
?>
谢谢...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)