foreach loop return a single result set instead of all result
I want to display the number of like and unlike a post has in a forum app. And this is how I create it.
<?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;?>
But to my surprise, it only displays the result of the last post likes and unlikes(meaning the rest are not shown) and I tried to put this inside a while loop and for loop but dont work as they hang the laptop.
The problem is in the template and not in the controller(so that is why am not showing the controller code)
The $votespost and $post(this is the post of each thread and that display fine) are from the controller class and I think they are working fine
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
]
];
}
I have commented out those not useful
php
add a comment |
I want to display the number of like and unlike a post has in a forum app. And this is how I create it.
<?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;?>
But to my surprise, it only displays the result of the last post likes and unlikes(meaning the rest are not shown) and I tried to put this inside a while loop and for loop but dont work as they hang the laptop.
The problem is in the template and not in the controller(so that is why am not showing the controller code)
The $votespost and $post(this is the post of each thread and that display fine) are from the controller class and I think they are working fine
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
]
];
}
I have commented out those not useful
php
What is$votespost
? Where do you assign that? Also what is$post
and why don't you just do the comparison in the query?
– user3783243
Nov 24 '18 at 23:26
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
Aggregation refers to collecting together rows of data into one row. As an example if you had a tablevotes
with columnspostid, voteup, votedown
(one row per vote), you might writeSELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…
– Nick
Nov 25 '18 at 0:15
add a comment |
I want to display the number of like and unlike a post has in a forum app. And this is how I create it.
<?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;?>
But to my surprise, it only displays the result of the last post likes and unlikes(meaning the rest are not shown) and I tried to put this inside a while loop and for loop but dont work as they hang the laptop.
The problem is in the template and not in the controller(so that is why am not showing the controller code)
The $votespost and $post(this is the post of each thread and that display fine) are from the controller class and I think they are working fine
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
]
];
}
I have commented out those not useful
php
I want to display the number of like and unlike a post has in a forum app. And this is how I create it.
<?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;?>
But to my surprise, it only displays the result of the last post likes and unlikes(meaning the rest are not shown) and I tried to put this inside a while loop and for loop but dont work as they hang the laptop.
The problem is in the template and not in the controller(so that is why am not showing the controller code)
The $votespost and $post(this is the post of each thread and that display fine) are from the controller class and I think they are working fine
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
]
];
}
I have commented out those not useful
php
php
edited Nov 25 '18 at 0:04
Gbenga Ogunbule
asked Nov 24 '18 at 23:18
Gbenga OgunbuleGbenga Ogunbule
446
446
What is$votespost
? Where do you assign that? Also what is$post
and why don't you just do the comparison in the query?
– user3783243
Nov 24 '18 at 23:26
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
Aggregation refers to collecting together rows of data into one row. As an example if you had a tablevotes
with columnspostid, voteup, votedown
(one row per vote), you might writeSELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…
– Nick
Nov 25 '18 at 0:15
add a comment |
What is$votespost
? Where do you assign that? Also what is$post
and why don't you just do the comparison in the query?
– user3783243
Nov 24 '18 at 23:26
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
Aggregation refers to collecting together rows of data into one row. As an example if you had a tablevotes
with columnspostid, voteup, votedown
(one row per vote), you might writeSELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…
– Nick
Nov 25 '18 at 0:15
What is
$votespost
? Where do you assign that? Also what is $post
and why don't you just do the comparison in the query?– user3783243
Nov 24 '18 at 23:26
What is
$votespost
? Where do you assign that? Also what is $post
and why don't you just do the comparison in the query?– user3783243
Nov 24 '18 at 23:26
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
Aggregation refers to collecting together rows of data into one row. As an example if you had a table
votes
with columns postid, voteup, votedown
(one row per vote), you might write SELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…– Nick
Nov 25 '18 at 0:15
Aggregation refers to collecting together rows of data into one row. As an example if you had a table
votes
with columns postid, voteup, votedown
(one row per vote), you might write SELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…– Nick
Nov 25 '18 at 0:15
add a comment |
1 Answer
1
active
oldest
votes
<?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
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463228%2fforeach-loop-return-a-single-result-set-instead-of-all-result%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
<?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
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
add a comment |
<?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
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
add a comment |
<?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
<?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
answered Nov 25 '18 at 0:26
Brian WhiteBrian White
1,26011115
1,26011115
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
add a comment |
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
What I want to do is match all votes with their corresponding post $vp->postId = $post->id eg if post->id is 3, search for that 3 in vote table using the column postId
– Gbenga Ogunbule
Nov 25 '18 at 0:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463228%2fforeach-loop-return-a-single-result-set-instead-of-all-result%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
$votespost
? Where do you assign that? Also what is$post
and why don't you just do the comparison in the query?– user3783243
Nov 24 '18 at 23:26
I suspect you need to perform aggregation in your query. You need to supply more information as to how you are generating the variables in this code.
– Nick
Nov 24 '18 at 23:30
@Nick have done that and what is aggregation?
– Gbenga Ogunbule
Nov 25 '18 at 0:05
Aggregation refers to collecting together rows of data into one row. As an example if you had a table
votes
with columnspostid, voteup, votedown
(one row per vote), you might writeSELECT postid, SUM(voteup) AS upvotes, SUM(votedown) AS downvotes FROM votes GROUP BY postid
to get the total up and down votes for each post. See this demo dbfiddle.uk/…– Nick
Nov 25 '18 at 0:15