[英]foreach loop return a single result set instead of all result
我想顯示論壇應用中帖子的喜歡和不喜歡的數量。 這就是我創建它的方式。
<?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
]
];
}
我已經注釋掉那些沒用的
<?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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.