1

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));
4
  • The final $lol is not even an array. I'm pretty sure array_sum won't work. Commented Dec 26, 2015 at 21:37
  • I did it here array_sum(array($lol)) Commented Dec 26, 2015 at 21:42
  • Sorry, didn't see that. And what did you get ? Commented Dec 26, 2015 at 21:44
  • it is only getting the value of the 1st item. it's not summing up the array Commented Dec 26, 2015 at 21:46

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.