CodeIgniter Forums
getting array values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: getting array values (/showthread.php?tid=13042)



getting array values - El Forum - 11-08-2008

[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


getting array values - El Forum - 11-08-2008

[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.