Reputation: 53
I just don't understand, I have code like this:
$this->ci->db->select('liked_posts, liked_comments');
$q = $this->ci->db->get_where('users_fav', array('usr_id' => $this->_usrId));
$result = $q->result_array();
And when I, as always, tried to put it into foreach loop.. it's just didn't work.. Because in $result I've got and array where 2 more arrays where stored (table fields) so to work in foreach loop it would look like this:
foreach($result[0] as $value)
not:
foreach($result as $value)
And I was looking for my mistake very long.. Maybe I really did something wrong... Or is it a bug?
edit: print_r($result);
Array
(
[0] => Array
(
[liked_posts] => a:0:{}
[liked_comments] => a:0:{}
)
)
edit2:
But shoudn't it be like this:
Array
(
[liked_posts] => a:0:{}
[liked_comments] => a:0:{}
)
?? Or I'm starting to go crazy???
edit3: My bad... I realized now... I'm just going crazy.. too much work done today... better go sleep :D Sorry guys
Upvotes: 0
Views: 74
Reputation: 7475
When using $result = $q->result_array();
you will get a multidimentional array as you have now.:
foreach( $resuls as $key => $each ){
echo "result : ".$each['column_name'];
}
but if you have just a single row fetched you would likely use $result = $q->row_array();
which will return a single dimentional array. And you can directly use like this:
echo $results['column_name'];
Upvotes: 0
Reputation: 2522
so you can do this
foreach($result as $value)
{
echo $value['fav_posts'];
}
no problem with that.
Upvotes: 2