CodeIgniter Forums
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;
foreach($d->result_array() as $row){
   $tot += $row['price'];
   ...
}

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:
public function do_the_query(){
  // DB stuff
  // ...
  return $query->result_array();
}

public function sum_prices($array){
  // calculate final price
  return $final_price;
}

// Controller
$db_result = $this->your_model->do_the_query();
$data['sum'] = $this->your_model->sum_prices($db_result);

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');
$query = $this->db->get('table_name');

echo $query->renamed_result_field; // Will prompt the sum of all prices in your table

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 Wink


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');
$query = $this->db->get('table_name'');

echo $query->renamed_result_field; // Will prompt the sum of all prices in your table

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 Wink[/quote]

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.  
$this->db->select_sum('price', 'renamed_result_field');
$query = $this->db->get_where('table', $where);

$results = $query->result();
return $results;





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 Wink
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('*');
        $this->db->from('borsellini');
        $this->db->join('clienti', 'clienti.id = borsellini.idcliente');
        $this->db->join('operazioni_borsellino', 'operazioni_borsellino.id_operazione_borsellino = borsellini.idoperazioneborsellino');
        $this->db->where('borsellini.idcliente', $this->uri->segment(3));

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');
because the query is not correct.
Apparently there are two solutions:

1. double query
2. a loop in controller to sum.

Sad




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('*');

$this->db->select('SUM(importo) as totale');

        $this->db->from('borsellini');
        $this->db->join('clienti', 'clienti.id = borsellini.idcliente');
        $this->db->join('operazioni_borsellino', 'operazioni_borsellino.id_operazione_borsellino = borsellini.idoperazioneborsellino');
        $this->db->where('borsellini.idcliente', $this->uri->segment(3));


But mysql returns different result.


I tried to add this code too
Code:
$this->db->select_sum('importo', 'totale');

but mysql returns different result.