我想显示论坛应用中帖子的喜欢和不喜欢的数量。这就是我创建它的方式。
<?php foreach ($votespost as $vp):?>
<?php if($post->id == $vp->postId):?>
<li><?=$vp->voteUp . ' Thumb up'?></li>
<li><?=$vp->voteDown . ' Thumb down'?></li>
<?php endif;?>
<?php endforeach;?>
但是令我惊讶的是,它只显示最后一个帖子点赞和不喜欢的结果(这意味着其余部分未显示),我试图将其放入while循环和for循环内,但由于它们挂起了笔记本电脑而无法正常工作。
问题出在模板而不是控制器中(这就是为什么不显示控制器代码的原因)
$ votespost和$ post(这是每个线程的帖子,显示得很好)来自控制器类,我认为它们工作正常
public function viewThread(){
if (isset($_GET['threadid'])){
//$thread = $this->threadsTable->findById($_GET['threadid']);
//$posts = $this->postsTable->find('threadId', $_GET['threadid']);
foreach ($posts as $post) {
if($post->id){
$votesPost = $this->votesTable->find('postId', $post->id);
}
}
}
$user = $this->authentication->getUser();
$title = 'View Topic';
return [
'template' => 'viewforum.html.php',
'title' => $title,
'variables' => [
//'thread' => $thread ?? null,
//'posts' => $posts ?? null,
'votespost' => $votesPost ?? null,
//'user' => $user ?? null
]
];
}
我已注释掉那些没用的
答案 0 :(得分:0)
<?php foreach ($votespost as $vp):?> <?php if($post->id == $vp->postId):?> <li><?=$vp->voteUp . ' Thumb up'?></li> <li><?=$vp->voteDown . ' Thumb down'?></li> <?php endif;?> <?php endforeach;?>. You have a foreach that iterates all the posts, but inside that you only output if the post id matches the vp postId. You're missing the else case