如何使帖子评论对自己的页面具有唯一性

zpqajqem  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(272)

这个问题在这里已经有了答案

如何在不同的页面上获得不同的评论(1个答案)
昨天关门了。
我有一个php博客,我正在尝试添加一个评论系统。我可以让评论系统工作,但是评论会出现在博客的每一篇文章上。
我应该在哪里插入代码,使每个帖子都有其独特的评论,而不是在每个帖子上显示相同的评论?我已经试过了,但没有做对。
以下是我尊敬的代码:
comment.php

  1. <h2><i>Drop a comment here</i></h2>
  2. <div class="comment-form-container">
  3. <form id="frm-comment">
  4. <div class="input-row">
  5. <input type="hidden" name="post_id" id="post_id" value="<?php echo $postID_id; ?>" />
  6. <input type="hidden" name="comment_id" id="commentId"
  7. placeholder="Name" /> <input class="input-field"
  8. type="text" name="name" id="name" placeholder="Name" required />
  9. </div>
  10. <div class="input-row">
  11. <textarea class="input-field" type="text" name="comment"
  12. id="comment" placeholder="Add a Comment" required > </textarea>
  13. </div>
  14. <div>
  15. <input type="button" class="btn-submit" id="submitButton"
  16. value="Publish" /><div id="comment-message">Comments Added Successfully!</div>
  17. </div>
  18. </form>
  19. </div>
  20. <div id="output"></div>
  21. <script>
  22. function postReply(commentId) {
  23. $('#commentId').val(commentId);
  24. $("#name").focus();
  25. }
  26. $("#submitButton").click(function () {
  27. $("#comment-message").css('display', 'none');
  28. var str = $("#frm-comment").serialize();
  29. $.ajax({
  30. url: "../comments/comment-add.php",
  31. data: str,
  32. data: {post_id : <?php echo $postID; ?>},
  33. type: 'post',
  34. success: function (response)
  35. {
  36. var result = eval('(' + response + ')');
  37. if (response)
  38. {
  39. $("#comment-message").css('display', 'inline-block');
  40. $("#name").val("");
  41. $("#comment").val("");
  42. $("#commentId").val("");
  43. $("#post_id").val("");
  44. listComment();
  45. } else
  46. {
  47. alert("Failed to add comments !");
  48. return false;
  49. }
  50. }
  51. });
  52. });
  53. $(document).ready(function () {
  54. listComment();
  55. });
  56. function listComment() {
  57. $.post("../comments/comment-list.php",
  58. function (data) {
  59. var data = JSON.parse(data);
  60. var comments = "";
  61. var replies = "";
  62. var item = "";
  63. var parent = -1;
  64. var results = new Array();
  65. var list = $("<ul class='outer-comment'>");
  66. var item = $("<li>").html(comments);
  67. for (var i = 0; (i < data.length); i++)
  68. {
  69. var commentId = data[i]['comment_id'];
  70. parent = data[i]['parent_comment_id'];
  71. if (parent == "0")
  72. {
  73. comments = "<div class='comment-row'>"+
  74. "<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>" +
  75. "<div class='comment-text'>" + data[i]['comment'] + "</div>"+
  76. "<div><a class='btn-reply' onClick='postReply(" + commentId + ")'>Reply</a></div>"+
  77. "</div>";
  78. var item = $("<li>").html(comments);
  79. list.append(item);
  80. var reply_list = $('<ul>');
  81. item.append(reply_list);
  82. listReplies(commentId, data, reply_list);
  83. }
  84. }
  85. $("#output").html(list);
  86. });
  87. }
  88. function listReplies(commentId, data, list) {
  89. for (var i = 0; (i < data.length); i++)
  90. {
  91. if (commentId == data[i].parent_comment_id)
  92. {
  93. var comments = "<div class='comment-row'>"+
  94. " <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>" +
  95. "<div class='comment-text'>" + data[i]['comment'] + "</div>"+
  96. "<div><a class='btn-reply' onClick='postReply(" + data[i]['comment_id'] + ")'>Reply</a></div>"+
  97. "</div>";
  98. var item = $("<li>").html(comments);
  99. var reply_list = $('<ul>');
  100. list.append(item);
  101. item.append(reply_list);
  102. listReplies(data[i].comment_id, data, reply_list);
  103. }
  104. }
  105. }
  106. </script>

comment-add.php

  1. <?php
  2. require_once ("db.php");
  3. $commentId = isset($_POST['comment_id']) ? $_POST['comment_id'] : "";
  4. $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : "";
  5. $comment = isset($_POST['comment']) ? $_POST['comment'] : "";
  6. $commentSenderName = isset($_POST['name']) ? $_POST['name'] : "";
  7. $date = date('Y-m-d H:i:s');
  8. $sql = "INSERT INTO tbl_comment(parent_comment_id,post_id,comment,comment_sender_name,date) VALUES ('" . $commentId . "','" . $post_id . "','" . $comment . "','" . $commentSenderName . "','" . $date . "')";
  9. $result = mysqli_query($conn, $sql);
  10. if (! $result) {
  11. $result = mysqli_error($conn);
  12. }
  13. echo $result;
  14. ?>

comment-list.php

  1. <?php
  2. require_once ("db.php");
  3. $qry2=mysqli_query($con,"select * from sa_posts where postID ='".$_GET['postID']."'");
  4. $post=mysqli_fetch_array($qry2);
  5. $sql = "SELECT * FROM tbl_comment ORDER BY parent_comment_id asc, comment_id asc";
  6. $result = mysqli_query($conn, $sql);
  7. $record_set = array();
  8. while ($row = mysqli_fetch_assoc($result)) {
  9. array_push($record_set, $row);
  10. }
  11. mysqli_free_result($result);
  12. mysqli_close($conn);
  13. echo json_encode($record_set);
  14. ?>
irtuqstp

irtuqstp1#

在comment-list.php中,在comments查询中添加post id。
从tbl_注解中选择*,其中post_id=“$_获取['postid']按父项注解id排序,注解id asc

zf9nrax1

zf9nrax12#

在comment-list.php中,您应该在查询中指定post_id。
因此改变

  1. "SELECT * FROM tbl_comment ORDER BY parent_comment_id asc, comment_id asc"

  1. $postID=$_GET['postID']; //or whatever the variable that your get the postID of the post.
  2. "SELECT * FROM tbl_comment where post_id=". $postID . " ORDER BY parent_comment_id asc, comment_id asc"

另一方面,函数listcoment()调用comment-list.php
但似乎没有传递给此函数的参数来指定post_id
因此,您应该将listcomment()修改为listcomment(post_id),然后将post_id作为say post或get参数传递给comment-list.php。

相关问题