我在做博客网页。用户可以在博客上发布他们的状态,因此在用户个人资料页面上,用户可以查看他们发布的内容。
我遇到了关于分开的帖子部分和评论部分的问题。 我想要的是显示带有帖子部分的评论部分。评论部分根据帖子部分显示。
示例:像facebook一样,我在该帖子上发布状态和其他用户评论。
Img 1如下:
在图1中,您可以看到根据帖子部分不显示评论部分。
Img 2如下:
这就是我想要做的例子。
PHP& HTML代码如下:
<div class="feed-card">
$postSql = "SELECT user.firstname, user.lastname, user.profile_photo, post.id ,post.post_contain, post.post_image, post.timestamp
FROM user
INNER JOIN post
ON user.id = post.user_id
WHERE user.id = '".$u_id."'
ORDER BY post.timestamp
DESC";
if ($postStmt = $conn->prepare($postSql)) {
/* execute query */
$postStmt->execute();
$postStmt->bind_result($user_firstname, $user_lastname, $user_profile_photo, $post_id, $post_content, $post_image, $post_timestamp);
$postIdArray = array();
while ($postStmt->fetch()) {
echo "post id: $post_id post content: $post_content";
echo '
<div class="media">
<div class="media-left">
<a class="media-object p-t-5">
<img src="' . $user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">
' . $user_firstname . ' ' . $user_lastname . '
</h4>
<small class="timestamp">' . $post_timestamp . '</small>
<p>' . $post_content . '</p>
</div>
<div class="comment-tools ml35">
<ul>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_like.png">Like</a></li>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_comment-no-apply.png"><span class="unselect">Comment</span></a></li>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_noapplyshare.png"><span class="unselect">Share</span></a></li>
</ul>
</div>
</div>
';
$postIdArray[] = $post_id;
}
$postStmt->close();
foreach ($postIdArray as $value){
getComments($conn,$value);
}
}
function getComments($conn,$postId){
$commentSql = " SELECT id, user_id, user_desc, timestamp FROM comments WHERE post_id = ? ";
if($commentStmt = $conn->prepare($commentSql)){
$commentStmt->bind_param("i", $postId);
$commentStmt->execute();
$commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp);
while ($commentStmt->fetch()) {
// echo "user dec: $userComment comment id: $commentId";
echo '
<div class="panel panel-radius">
<div class="panel-heading panel-custom-event custom-donut">
<p><img src="/img/people_liked.png">
</p>
</div>
<div class="panel-body body-panel no-border">
<hr class="custom-hr">
<div class="media-left">
<a href="#">
<img src="' . $row->profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $row->firstname . ' ' . $row->lastname . '</h4>
<p>' . $userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $commentId . '"><a href="#" onclick="return false;">Like</a></li>
<li id="" value="' . $commentId . '"><a href="" onclick="return false;"><span class="unselect" id="comment_reply">Reply</span></li>
<li id="" value="' . $commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
<li>
<small class="timestamp">' . $comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
<div class="media m-b-20">
<div class="media-left">
<a href="#">
<img src="' . $user_profile_photo . '" class="reply_image" /></img>
</a>
</div>
<div class="media-body">
<div class="right-addon">
<input type="text" class="form-control addradius" id="reply">
</div>
</div>
</div>
</div>
</div>
';
}
$commentStmt->close();
return $userComment;
}else{
return "error preparing sql";
}
}
答案 0 :(得分:0)
不是收集数组中的所有帖子ID,然后为每个帖子获取评论,而是在显示帖子时获取每个帖子的评论。我无法测试代码,但这应该可行。
<div class="feed-card">
<?php
$postSql = "SELECT user.firstname, user.lastname, user.profile_photo, post.id ,post.post_contain, post.post_image, post.timestamp
FROM user
INNER JOIN post
ON user.id = post.user_id
WHERE user.id = '".$u_id."'
ORDER BY post.timestamp
DESC";
if ($postStmt = $conn->prepare($postSql)) {
/* execute query */
$postStmt->execute();
$postStmt->bind_result($user_firstname, $user_lastname, $user_profile_photo, $post_id, $post_content, $post_image, $post_timestamp);
$postIdArray = array();
while ($postStmt->fetch()) {
echo "post id: $post_id post content: $post_content";
echo '
<div class="media">
<div class="media-left">
<a class="media-object p-t-5">
<img src="' . $user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">
' . $user_firstname . ' ' . $user_lastname . '
</h4>
<small class="timestamp">' . $post_timestamp . '</small>
<p>' . $post_content . '</p>
</div>
<div class="comment-tools ml35">
<ul>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_like.png">Like</a></li>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_comment-no-apply.png"><span class="unselect">Comment</span></a></li>
<li id="' . $post_id . '"><a href="#"><img src="/img/ic_noapplyshare.png"><span class="unselect">Share</span></a></li>
</ul>
</div>
</div>
';
//$postIdArray[] = $post_id;
getComments($conn,$post_id);
}
$postStmt->close();
//foreach ($postIdArray as $value){
// getComments($conn,$value);
//}
}
function getComments($conn,$postId){
$commentSql = " SELECT id, user_id, user_desc, timestamp FROM comments WHERE post_id = ? ";
if($commentStmt = $conn->prepare($commentSql)){
$commentStmt->bind_param("i", $postId);
$commentStmt->execute();
$commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp);
while ($commentStmt->fetch()) {
// echo "user dec: $userComment comment id: $commentId";
echo '
<div class="panel panel-radius">
<div class="panel-heading panel-custom-event custom-donut">
<p><img src="/img/people_liked.png">
</p>
</div>
<div class="panel-body body-panel no-border">
<hr class="custom-hr">
<div class="media-left">
<a href="#">
<img src="' . $row->profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $row->firstname . ' ' . $row->lastname . '</h4>
<p>' . $userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $commentId . '"><a href="#" onclick="return false;">Like</a></li>
<li id="" value="' . $commentId . '"><a href="" onclick="return false;"><span class="unselect" id="comment_reply">Reply</span></li>
<li id="" value="' . $commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
<li>
<small class="timestamp">' . $comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
<div class="media m-b-20">
<div class="media-left">
<a href="#">
<img src="' . $user_profile_photo . '" class="reply_image" /></img>
</a>
</div>
<div class="media-body">
<div class="right-addon">
<input type="text" class="form-control addradius" id="reply">
</div>
</div>
</div>
</div>
</div>
';
}
$commentStmt->close();
return $userComment;
}else{
return "error preparing sql";
}
}
?>
</div>