PHP post 并且评论未在组中显示

PHP post and comment is not showing in group

我正在尝试获取 posts 及其评论,但它总是显示 post 每次重复一个评论 请看图片

这是我试过的

test.php

$q = "SELECT posts.*, comments.*
      FROM posts
      LEFT JOIN comments
      ON comments.postid= posts.postid ";

$result = mysqli_query($connection, $q);

while($fetch=mysqli_fetch_array($result)){
    echo "<h5>".$fetch['post']."</h5>";
    echo "<p><i class='fa fa-user'></i> ".$fetch['comment']."</p>";
    echo "<textarea class='form-control' name = 'comment' placeholder='Write Comment'></textarea>
    <input type='submit' class='btn btn-lg' value='Add Comment!'>";    
}

这是我的数据库

我想做的是显示所有与之相关的评论post,但不起作用

我试过这个功能,但它没有显示第一条评论,总是从第二条评论开始

functions.php

function getPostsInfoo(){
$q = "SELECT posts.*, comments.*
     FROM posts
     LEFT JOIN comments ON comments.postid=posts.postid";
$result = mysqli_query($this->connection, $q);
$posts = [];
while($fetch=mysqli_fetch_array($result)){
    if(isset($posts[$fetch["postid"]])){
        $posts[$fetch["id_cmnt"]]["comment"][] = [
            "username" => $fetch["username"],
            "comment" => $fetch["comment"],
            "cmntdate" => $fetch["cmntdate"]
        ];
        echo "<p><a href='#'> ".$fetch["username"]."</a> ".$fetch["comment"]."</p>";
    }else{
        $posts[$fetch["postid"]] = [
            "post" => $fetch["post"],
            "username" => $fetch["username"],
            "comment" => [[
                "username" => $fetch["username"],
                "comment" => $fetch["comment"],
                "cmntdate" => $fetch["cmntdate"]
            ]]
        ];
        echo "<p ><a href='#'>".$fetch["username"]."</a>".$fetch["post"]."</p>";
    }
}
return $posts;

}

test.php

$database->getPostsInfoo(); 我只添加了这个并且它有效但看到了结果

我找到了解决方案

$q = "SELECT posts.*, comments.*
      FROM posts
      LEFT JOIN comments
      ON comments.postid= posts.postid ";

      $result = mysqli_query($this->connection,$q);
      $previous_post = 0;
      while ($data = mysqli_fetch_array($result)) {
           if ($previous_post != $data['postid']) {
               echo '<h1>' . $data['post'] . '</h1>';
                $previous_post = $data['postid'];
           }
           echo '<p>' . $data['comment'] . '</p>';
      }