Say you construct an activerecord query that will always just return a single value, how do you just address that single value instead of getting an array in return? For instance I am using an ActiveRecord query to return the SUM of a single column, it will only return this one single SUM, instead of having to parse the array is there a way to assign the value as a function return equal to that value instead of getting an array?
views:
461answers:
2
A:
this isn't the prettiest way. i can't find a way to do it with the active record api. but here's a one-liner to get it.
$result = array(
array('sum' => '23')
);
echo current( current( $result ) );
Galen
2010-04-20 03:47:37
Wouldnt that set a value in an array?
txmail
2010-04-21 01:18:49
+3
A:
$query = $this->db->get(); // your ActiveRecord query that will return a col called 'sum'
echo $query->row('sum');
bjornbjorn
2010-04-20 05:26:35
This is the right answer for Active Record, but in general if you wanted to store the first result of any array into a var you could use something like: $var = whatever(); $var = $var[0];and then from that point on you don't have to mess with indices.
njbair
2010-04-23 21:51:51