CodeIgniter Forums
Select Sum and Undefined Property Issue - 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: Select Sum and Undefined Property Issue (/showthread.php?tid=6515)



Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]blupixelz[/eluser]
I'm new to MVC and IC and I need some help with "select sum."

I have the following code and I keep on getting the following error - "Undefined property: CI_DB_mysql_result::$total_price"

I thought I had already defined a new property name 'total_price'...

This is what I want it to do in MySQL...
Code:
SELECT SUM(price) AS 'total_price' FROM movies
WHERE year(date_added) = '2007'

And I'm sure that this is exactly same as the MySQL statement above.
Code:
$this->db->select_sum('movies.price','total_price');
$this->db->where('year(movies.date_added)',$_POST['year']);
$data['query_total'] = $this->db->get('movies');

But why is my IC code causing an error in View?
Code:
<?=$query_total->total_price?>



Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]Doosje[/eluser]
Probably you have more rows ..
So either you need to loop or use the first one in the loop


Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]blupixelz[/eluser]
When I run the SQL statement in phpMyAdmin, I only get one row as a result

MySQL Query
Code:
SELECT SUM(price) AS 'total_price' FROM movies
WHERE year(date_added) = '2007'

Result
Code:
total_price
50.00

I'm still puzzled by this... considering how simple the query is...


Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]Doosje[/eluser]
Code:
$this->db->get('movies');
returns an array .. even if the result is one
So you can do:
Code:
$query_total[0]->total_price;



Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]blupixelz[/eluser]
Thanks for the quick reply!

What you are saying makes sense, but I'm getting this error message now.

Line 176:
Code:
<td><b>$&lt;?=$query_total[0]->total_price?&gt;</b></td>

Error Message:
Code:
$
Fatal error: Cannot use object of type CI_DB_mysql_result as array in C:\lighttpd\htdocs\codeigniter\system\application\views\totalamountspent_view.php on line 176



Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]Doosje[/eluser]
I'm watching now better and see that you are missing ->result();
So
Code:
$this->db->select_sum('movies.price','total_price');
$this->db->where('year(movies.date_added)',$_POST['year']);
$query = $this->db->get('movies');
THEN
Code:
$data['query_total'] = $query->result();
OR
Code:
$data['query_total'] = $query->row();

If you only care about one result then do the row thing .. and
Code:
$query_total->total_price;

else .. loop thru the array
or use
Code:
$query_total[0]->total_price;



Select Sum and Undefined Property Issue - El Forum - 03-02-2008

[eluser]blupixelz[/eluser]
Your advice did the trick!

What I have learned from you is that result() works only in loops (from View) - since the function typically expects there are more than one array in an object.

To summarize:
1. In View, use result() function in loop.
OR
2. In Controller, use result() function before variables reach View.

Thank you for your help.


Select Sum and Undefined Property Issue - El Forum - 05-08-2012

[eluser]Unknown[/eluser]
Four years later, this works. Thanks guys.


Select Sum and Undefined Property Issue - El Forum - 05-08-2012

[eluser]InsiteFX[/eluser]
DB results
Code:
// Multiple Rows:
result()       - returns a array of objects - use foreach loop
result_array() - returns a pure array['']   - use foreach loop

// Single Row:
row()          - returns a single row object
row_array()    - returns a single row array['']