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 And I'm sure that this is exactly same as the MySQL statement above. Code: $this->db->select_sum('movies.price','total_price'); 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 Result Code: total_price 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'); 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>$<?=$query_total[0]->total_price?></b></td> Error Message: Code: $ 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'); Code: $data['query_total'] = $query->result(); 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: |