• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select Sum and Undefined Property Issue

#1
[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?>

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

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

#4
[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;

#5
[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

#6
[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;

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

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

#9
[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['']


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.