Reputation: 333
The result stored in $g
is 1 and 2. The following code I've written below, my $array['music']
stores only the last element that is 2. But what I want is to execute my sql query 2 times under foreach and match the value of $g
which is 1 and 2 with mu_id
(mu_id
is the column name from another table music
) and store all the rows data of row 1 and 2 in to $array['music']
.
It is storing only for the second row (2) not for 1 or it is overwriting it when it is executing for the second time inside loop. If there is any logic to make it work then please let me know.
foreach($genre as $g)
{
echo $g;
echo "<br>";
$array['music'] = $m -> where('mu_id', $g ) -> get();
}
Upvotes: 0
Views: 939
Reputation: 1268
If you want to store all the data in an array you should be using $array['music'][]
instead of $array['music']
Upvotes: 0
Reputation: 102745
You're redeclaring the entire array each time rather than adding to it, use this instead:
foreach($genre as $g)
{
$array['music'][] = $m->where('mu_id', $g)->get();
}
Or even better, less queries:
$array['music'] = $m->where_in('mu_id', $genre)->get();
Upvotes: 6