![]() |
What's the correct mode to sum a value? Controllers or view? - 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: What's the correct mode to sum a value? Controllers or view? (/showthread.php?tid=52832) |
What's the correct mode to sum a value? Controllers or view? - El Forum - 06-28-2012 [eluser]satie[/eluser] I have a list in a view Code: $tot = 0; What's the correct mode to calculate the sum? I'd repeat foreach in controller and send $tot var to view? Thanks What's the correct mode to sum a value? Controllers or view? - El Forum - 06-28-2012 [eluser]LuckyFella73[/eluser] I guess the best way would be to get the database entries in your model and calculate the sum in the model as well. Could look like this (just one way to do it!). Code: // Model: I'm sure you could sum the prices with a sql query but I'm not that good at building such queries .. What's the correct mode to sum a value? Controllers or view? - El Forum - 06-28-2012 [eluser]PhilTem[/eluser] [quote author="LuckyFella73" date="1340899396"]I'm sure you could sum the prices with a sql query but I'm not that good at building such queries ..[/quote] Code: $this->db->select_sum('price', 'renamed_result_field'); Probably the fastest version (as long as you don't have such large db data) than running it within the view or a separate foreach-loop ![]() What's the correct mode to sum a value? Controllers or view? - El Forum - 06-28-2012 [eluser]rainman[/eluser] [quote author="PhilTem" date="1340904008"][quote author="LuckyFella73" date="1340899396"]I'm sure you could sum the prices with a sql query but I'm not that good at building such queries ..[/quote] Code: $this->db->select_sum('price', 'renamed_result_field'); Probably the fastest version (as long as you don't have such large db data) than running it within the view or a separate foreach-loop ![]() Agree'd with the above. I use the model for items like that. You could do the following as well: Code: $where = array('fieldname' => 'value'); // I like using this method so I can edit my query quickly. What's the correct mode to sum a value? Controllers or view? - El Forum - 06-29-2012 [eluser]LuckyFella73[/eluser] @PhilTem Code: $this->db->select_sum('price', 'renamed_result_field'); "Knew" it must be that simple ![]() I read parts of the User Guide nearly every day but never noticed that one - lol What's the correct mode to sum a value? Controllers or view? - El Forum - 06-29-2012 [eluser]satie[/eluser] Thanks for answer This is my query Code: $this->db->select('*'); I would avoid double query: one for data list and one for sum. I can't add, actually, Code: $this->db->select_sum('importo', 'totale'); Apparently there are two solutions: 1. double query 2. a loop in controller to sum. ![]() What's the correct mode to sum a value? Controllers or view? - El Forum - 06-29-2012 [eluser]CroNiX[/eluser] Is there a reason why you can't add another join on your importo table in the query and perform a sum for each client that way? What's the correct mode to sum a value? Controllers or view? - El Forum - 06-29-2012 [eluser]satie[/eluser] I tried to add sum Code: $this->db->select('SUM(importo) as totale'); in my query Code: $this->db->select('*'); But mysql returns different result. I tried to add this code too Code: $this->db->select_sum('importo', 'totale'); but mysql returns different result. |