Welcome Guest, Not a member yet? Register   Sign In
Why is this function returing an Object Id?
#1

[eluser]Solarpitch[/eluser]
Hey,

I have a simple model function to return the sum of this column, however all it returns is Object id #14.

I know I'm probably not getting the result set correctly but I'm not sure how? I take it you dont need a loop to get the result set?

This is what I have...

Code:
function test()
    {
    
    $this->db->select_sum('price');
    $query = $this->db->get('sales');

    $row = $query->row();
    
    return $row;

    }
#2

[eluser]NogDog[/eluser]
I think you want something like:
Code:
function test()
{
   $this->db->select_sum('price', 'price_total');
   $query = $this->db->get('sales');
   $row = $query->row();
   return $row->price_total;
}
#3

[eluser]Solarpitch[/eluser]
Thanks that seems to work. Just so I understand it... what does the SQL produce because I dont understand why we need to include the price_total into the SQL?
#4

[eluser]NogDog[/eluser]
The main thing is that the row() method returns an object with a member variable for each column in the query result row. I added the optional 2nd parameter to the select_num() method so that I would know for sure what the summed column was called (this would be an alias in the SQL, e.g. "...SUM(price) AS price_total..."). Then you just need to specify which column you want from the result row() and return it (instead of the entire object).
#5

[eluser]Solarpitch[/eluser]
Perfect. I understand that now.

Many Thanks!
#6

[eluser]xwero[/eluser]
You don't need the price_total but it's a visual cue to see you got the sum of the price field. Most of the time it's used if you don't need the value not direct under the db query.
Code:
$this->db->select_sum('price', 'price_total');
   $query = $this->db->get('sales');
   $row = $query->row();
   // 20 more lines of code
   return $row->price_total;

The main thing is that if you want to return a column value you have to call it. If you are using php5 you can chain the methods making your method code
Code:
return $this->db->select_sum('price')->get('sales')->row()->price;

edit : TOO SLOW Smile




Theme © iAndrew 2016 - Forum software by © MyBB