简体   繁体   English

如何在帖子上显示评论

[英]how to display comments on post

I've small blog users can add new post and others can comment on this post ( Post is working perfectly, comments is working too )我的小博客用户可以添加新帖子,其他人可以对这篇文章发表评论(帖子运行良好,评论也运行良好)

all posts is showing and all comment is showing but each Separated , how can I display it related.所有帖子都在显示,所有评论都在显示,但每个分离,我如何显示它相关。

function getPostInfoo(){
  if($this->num_members < 0){
    $q = "SELECT * FROM ".TBL_POSTS;
    $result = mysqli_query($this->connection, $q);
    $fields = array();

    while($fetch=mysqli_fetch_array($result)){
        echo "
        <h3>".$fetch['post']."</h3>
        <p>".$fetch['username']."</p>";
    }
    $this->num_members = mysqli_fetch_array($result);
  }
  //return
  return $fields;
}
function getComment(){

  $q = "SELECT ".TBL_POSTS.".*, ".TBL_COMMENTS.".*
         FROM ".TBL_POSTS."
         INNER JOIN ".TBL_COMMENTS." ON ".TBL_COMMENTS.".postid=".TBL_POSTS.".postid";
  $result = mysqli_query($this->connection, $q);
  /* Error occurred, return given name by default */
  if(!$result || (mysqli_num_rows($result) < 1)){
     return NULL;
  }
  $fields = array();
  while($fetch=mysqli_fetch_array($result)){
      $fields[] = "<p><a href='#'>".$fetch['username'].": </a>".$fetch['comment']."<br>
                      <small class='text-muted'>".$fetch['cmntdate']."</small>
                    </p>

      ";
   }
  /* Return result array */
  return $fields;
 }

test.php测试文件

echo "<div>";
$myList2 = $database->getPostInfoo();
if (is_array($myList2) || is_object($myList2)){
  foreach($myList2 as $arrayItem2){
    echo $arrayItem2;

  }
}else {
     echo      "No Posts yet.";
}
echo "</div>
<div>";
$myList = $database->getComment();
if (is_array($myList) || is_object($myList)){
    foreach($myList as $arrayItem){
        echo $arrayItem;
    }
}else {
   echo "No comments yet.";
}

<form method='post' action='functions.php' method='POST'>
<div class='form-group' >
    <textarea class='form-control' name = 'comment' placeholder='Write Comment'></textarea>
    <input type='hidden' name='postid' value='1'>
    <input type='hidden' name='comment_sub' value='1'>
    <input type='submit' value='Add Comment!'>
</div>
</form>

</div>";

what I need to do is to display each comments on its own post & show comment textarea on each post我需要做的是在自己的帖子上显示每个评论并在每个帖子上显示评论文本区域

Please see the image https://i.stack.imgur.com/A4D55.jpg请看图片https://i.stack.imgur.com/A4D55.jpg

You need to be getting one post at a time and then fetch its comments.您需要一次收到一篇文章,然后获取其评论。

So your test.php should look this test.php?id=post_id所以你的test.php应该看起来像test.php?id=post_id

Where post_id is the unique identifier of your post based on your db structure其中post_id是基于您的数据库结构的帖子的唯一标识符

Could be eg is 1 or 2 or if you are using auto-increment id可能是例如是 1 或 2 或者如果您使用自动增量 ID

Then you can add the following code to at the beginning of your test.php to get the post id然后你可以在你的test.php的开头添加下面的代码来获取 post id

$post_id = isset($_GET['id']) ? $_GET['id'] : '';

The fetch the post and it's comment from your table using the value of $post_id使用$post_id的值从您的表中获取帖子及其评论

Hope it helps希望能帮助到你

Try this function:试试这个功能:

function getPostsInfo(){
$q = "SELECT ".TBL_POSTS.".`id`,".TBL_POSTS.".`post`, ".TBL_POSTS.".`username`, ".TBL_COMMENTS.".`username` as comment_username, ".TBL_COMMENTS.".`comment`, ".TBL_COMMENTS.".`cmntdate` 
     FROM ".TBL_POSTS."
     LEFT JOIN ".TBL_COMMENTS." ON ".TBL_COMMENTS.".postid=".TBL_POSTS.".postid";
$result = mysqli_query($this->connection, $q);
$posts = [];
while($fetch=mysqli_fetch_array($result)){
    if(isset($posts[$fetch["id"]])){
        $posts[$fetch["id"]]["comments"][] = [
            "username" => $fetch["comment_username"],
            "comment" => $fetch["comment"],
            "cmntdate" => $fetch["cmntdate"]
        ];
    }else{
        $posts[$fetch["id"]] = [
            "post" => $fetch["post"],
            "username" => $fetch["username"],
            "comments" => [[
                "username" => $fetch["comment_username"],
                "comment" => $fetch["comment"],
                "cmntdate" => $fetch["cmntdate"]
            ]]

        ];
    }
}
return $posts;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM