Aaron Alfonso
Aaron Alfonso

Reputation: 516

PHP: Sum of Array

I'm having a difficulty getting the sum of the array. Since I have a different formula, I can't use the codeigniter's function select_sum()

The code below is my attempt, but I have failed to make it functional.

$this->db->where('request_id', $this->input->post('request_id'));  
              $this->db->join('medical_request_items', 'items.item_id=medical_request_items.item_id');                  
              $quer2 = $this->db->get('items');

                    foreach ($quer2->result() as $row) {                            


                         $lol = (($row->item_quantity * $row->item_retailprice) - ($row->item_quantity * $row->discount) - ($row->item_quantity * $row->philhealth) - ($row->item_quantity * $row->senior));

                    }


                    echo array_sum(array($lol));

Upvotes: 1

Views: 795

Answers (1)

frz3993
frz3993

Reputation: 1635

Assuming your query and formula don't have any problems and work as they should. Declare $lol as an array before the loop

$lol = array();

In the loop

$lol = ((.....

should be

$lol[] = ((...

Then, you just echo the sum

 echo array_sum($lol);

Upvotes: 2

Related Questions