简体   繁体   English

如何显示帖子及其评论?

[英]How to display a post and its comments?

I want to view all posts, all comments for each post at once.我想一次查看所有帖子,每个帖子的所有评论。 While I have no problems with displaying posts, displaying comments is already a problem.虽然我在显示帖子方面没有问题,但显示评论已经是一个问题。 One post can have multiple comments, so I have no idea how to create a query to display this.一篇文章可以有多个评论,所以我不知道如何创建一个查询来显示这个。 I tried to use LEFT JOIN but it didn't help.我尝试使用 LEFT JOIN 但没有帮助。 I include a table schema below the code to make my problem easier to understand.我在代码下方包含了一个表模式,以使我的问题更容易理解。

 <?php foreach ($tweet->userData as $user) {?> <div class='col-xl-8' id='posty'> <div class='row' id='time'> <div class='btn-block d-flex justify-content-between'> <div class='imie'> <?php echo $user['autor']?> </div> <div class='czas'> <?php echo $user['date_to_add']?> </div> </div> </div> <?php echo $user['comment']?> <form action='' method='post' id="myForm"> <div class='row'> <div class='col-12 col-xl-12 d-flex justify-content-between' id='icon'> <button class='button2' name='dodaj_like' style="background-color: <?php if($user['like_color']==1){echo '#00FA9A';}else{echo 'black';}?>" ><i class='fas fa-heart' ></i><input type='hidden' name='like' value="<?php echo $user['id']?>" /><span id="font"><?php echo $user['likes']?></span></button> <button class='button2' name='dodaj_dislike' style="background-color: <?php if($user['dislike_color']==1){echo '#00FA9A';}else{echo 'black';}?>"><i class='fas fa-heart-broken'></i><input type='hidden' name='dislike' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['dislikes']?></span></button> <button class='button2' name='dodaj_comment' id="com" ><i class='far fa-comment-dots'></i><input type='hidden' name='comment' value="<?php echo $user['id']?>"/><span id="font">Comment</span></button> <button class='button2' name='dodaj_share' style="background-color: <?php if($user['share_color']==1){echo '#00FA9A';}else{echo 'black';}?>" ><i class='far fa-share-square' ></i><input type='hidden' name='share' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['shares']?></span></button> </div> </div> </form> <div class="row d-flex"> <div class="col-xl-12 bg-success "> <form method="post"> <textarea id="form103" class="md-textarea form-control" rows="5" placeholder="Co słychać?" name="komentarz"></textarea> <div> <?php echo $aabbcc?? ''?> </div> <div class="button"> <button class="btn btn-danger mt-2" name='dodaj_comment'><input type='hidden' name='com' value="<?php echo $user['id']?>"/>Publikuj</button> </div> </form> </div> </div> </div> <?php }?>

$id = $_GET['id'];
$session = $_SESSION['id'];
$sql = $this->database->connect()->prepare("SELECT post.id, CONCAT(first_name,' ', last_name) AS author, post.comment, post.date_to_add, post_comment.comment, post_comment.date_to_add FROM user JOIN post ON user.id = post.user_id  LEFT JOIN post_comment ON post.user_id=post_comment.post_id where post.user_id = :user_id order by post.id DESC");
$sql->bindParam(':user_id',$id, PDO::PARAM_INT);
$sql->bindParam(':id',$session, PDO::PARAM_INT);
$sql->execute();

    if($sql->rowCount())
    {
        $this->userData = [];

        while ($row = $sql->fetch())
        {
            $this->userData[] = $row;
        }
    }
}

在此处输入图像描述

You can get the Comment form database for specific Post using post_id by selecting Comment Table:您可以通过选择评论表使用post_id获取特定帖子的评论表单数据库:

$query="select * form post_comment where post_id=". $post_id;

here is the PHP Implementation:这是 PHP 实现:

 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: ". $conn->connect_error); } $sql_post = "SELECT * FROM post"; $result = $conn->query($sql_post ); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<br> post id: ". $row["id"]. " - <br> Post: ". $row["post"]<br>"; $comment_query=$conn->query("select * form post_comment where post_id=". $row["id"]); if ($comment_query->num_rows > 0) { // output data of each row while($comment= $result->fetch_assoc()) { echo " <p>Comment ".comment['comment']." </p>"; } } } } else { echo "0 results"; } $conn->close();

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

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