我试图找到每个帖子上发表的评论数量,并且在用户退出时工作正常,但是如果他们已经登录了它的话奇怪的东西。
如果他们已登录,我会检查他们是否已投票或投票,但我已将其置于if语句中。也许它有冲突?
<?php
if (isset($_SESSION['userid'])){
$stmt = $db->prepare("SELECT pv_type,COUNT(*) FROM post_votes WHERE pv_postid = ? AND pv_userid = ?");
}
$stmt2 = $db->prepare("SELECT COUNT(*) FROM comments WHERE c_postid=?");
foreach($posts as $post):
// [variables set here, removed for this post]
if (isset($_SESSION['userid'])){
$stmt->bind_param("ii",$p_id,$session_id);
$stmt->execute();
$stmt->bind_result($pv_type,$voted);
$stmt->fetch();
}
$stmt2->bind_param("i",$p_id);
$stmt2->execute();
$stmt2->bind_result($numComments);
$stmt2->fetch();
if ($numComments == 1){
$numComments = $numComments . " comment";
} else {
$numComments = $numComments . " comments";
}
?>
** HTML to be rendered **
<div class="comment-info"><?php echo $numComments; ?></div>
<?php
endforeach;
if (isset($_SESSION['userid'])){
$stmt->close();
}
$stmt2->close();
当用户登录时,它不会获得评论数量,并且在每个帖子上都会添加其他评论,例如
帖子1为comments
,帖子2为comments comments
,而不是1 comment
,3 comments
等。
我必须在这里遗漏一些东西