簡體   English   中英

如何在帖子上顯示評論

[英]how to display comments on post

我的小博客用戶可以添加新帖子,其他人可以對這篇文章發表評論(帖子運行良好,評論也運行良好)

所有帖子都在顯示,所有評論都在顯示,但每個分離,我如何顯示它相關。

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;
 }

測試文件

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>";

我需要做的是在自己的帖子上顯示每個評論並在每個帖子上顯示評論文本區域

請看圖片https://i.stack.imgur.com/A4D55.jpg

您需要一次收到一篇文章,然后獲取其評論。

所以你的test.php應該看起來像test.php?id=post_id

其中post_id是基於您的數據庫結構的帖子的唯一標識符

可能是例如是 1 或 2 或者如果您使用自動增量 ID

然后你可以在你的test.php的開頭添加下面的代碼來獲取 post id

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

使用$post_id的值從您的表中獲取帖子及其評論

希望能幫助到你

試試這個功能:

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