Welcome Guest, Not a member yet? Register   Sign In
getting array values
#1

[eluser]Fenix[/eluser]
I'd like to clear up (for me) how to access array results from a query in the most efficient/easy way. When I run a model function like this:

Code:
function get_values($post_id)
{
   $this->db->select('item1, item2, item3');
   $this->db->where('post_id', $post_id);
   $this->db->limit(1);
   return $this->db->get('posts')->result();
}

This way:

Code:
$result = $this->mymodel->get_values()

I'd like to be able to access the values like this:
Code:
$result->item1;
$result->item2;
$result->item3;

// or

$result['item1'];
$result['item2'];
$result['item3'];

Can anybody explain how to do this or something like this? Can anybody tell me what I'm doing wrong with either my model function or anything else?

Thanks
#2

[eluser]Armchair Samurai[/eluser]
If you're just getting a single entry from your DB, use row() or row_array() instead of result() or result_array(). The former will return a single object or array, rather than an array of objects/arrays.




Theme © iAndrew 2016 - Forum software by © MyBB