我使用foreach循环选择所有帖子,并成功返回所有结果。之后,我创建了一个注释函数来附加到所有帖子,但是当我使用foreach循环选择所有结果时它只返回我最新的结果。
问题是我创建函数来选择每个循环使用的comment_table的所有结果。结果只返回1个结果但是在数据库gt中两个结果,它应该返回2个结果。之后,我调用该函数并在另一个foreach循环块中回显它。
PHP和HTML代码(评论部分):
function getComments($conn,$postId){
$commentSql = " SELECT comments.id, comments.user_id, comments.user_desc, comments.timestamp, user.firstname, user.lastname, user.profile_photo
FROM comments
INNER JOIN user
ON user.id = comments.user_id
WHERE post_id = ? ";
if($commentStmt = $conn->prepare($commentSql)){
$commentStmt->bind_param("i", $postId);
$commentStmt->execute();
$commentStmt->bind_result($commentId, $userId, $userComment, $comment_timestamp, $user_firstname, $user_lastname, $user_profile_photo);
$commentIdArray = array();
while ($commentStmt->fetch()) {
$object1 = new stdClass();
$object1->commentId = $commentId;
$object1->userId = $userId;
$object1->userComment = $userComment;
$object1->comment_timestamp = $comment_timestamp;
$object1->user_firstname = $user_firstname;
$object1->user_lastname = $user_lastname;
$object1->user_profile_photo = $user_profile_photo;
$commentIdArray[] = $object1;
}
$commentStmt->close();
$count = 0;
foreach ($commentIdArray as $value1){
$test = $value1;
$comments = '
<div class="media-left">
<a href="#">
<img src="' . $value1->user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $value1->user_firstname . ' ' . $value1->user_lastname . '</h4>
<p>' . $value1->userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $value1->commentId . '"><a href="#" onclick="return false;">Like</a></li>
<li id="comment_reply'.$count.'" value="' . $value1->commentId . '" onclick=" getidforeachbutton(this.id);"><a href="" onclick="return false;"><span class="unselect">Reply</span></li>
<li id="" value="' . $value1->commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
<li>
<small class="timestamp">' . $value1->comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
';
++$count;
}
print_r($test);
return $comments;
}else{
return "error preparing sql";
}
}
评论表:
结果:
答案 0 :(得分:0)
更改:
foreach ($commentIdArray as $value1){
$test = $value1;
$comments = ...
致:
foreach ($commentIdArray as $value1){
$test = $value1;
$comments .= ...
答案 1 :(得分:0)
你的$comments
应该是一个返回多个值的数组,否则,它只返回最后一个值。所以你的注释变量就像函数内部一样:
$comments[] =
答案 2 :(得分:0)
应该是:
$comments.= '
<div class="media-left">
<a href="#">
<img src="' . $value1->user_profile_photo . '" class="img-rounded custom-birthday-image" /></img>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"> ' . $value1->user_firstname . ' ' . $value1->user_lastname . '</h4>
<p>' . $value1->userComment . '</p>
<div class="comment-tools">
<ul id="c_option">
<li id="c_like" value="' . $value1->commentId . '"><a href="#" onclick="return false;">Like</a></li>
<li id="comment_reply'.$count.'" value="' . $value1->commentId . '" onclick=" getidforeachbutton(this.id);"><a href="" onclick="return false;"><span class="unselect">Reply</span></li>
<li id="" value="' . $value1->commentId. '"><a href="#"><img src="/img/people_liked.png"></a></li>
<li>
<small class="timestamp">' . $value1->comment_timestamp . '</small>
</li>
</ul>
</div>
</div>
';