表中多列的总和

I am currently having problem with my total. Well, I have 3 different tables. billing_payments, billing_entry, and services.

My problem is, I can't get the total of each SERVICES. I provided a screenshot so that you'll get what I mean.

enter image description here

Here is the code of the report. I have added some comments to indicate where the problem starts and ends.

<table class="table table-striped table-hover">
              <thead>
                <tr>
                <th>#</th>                    
                <th>PATIENT NAME</th>
                <th>CASE</th>                                    
                <?php
                            $servicesquery = $this->db->get('services');
                            foreach ($servicesquery->result() as $service) {
                              echo '<th>'.$service->service_name.'</th>';
                            }
                ?>
                <th>MEDICAL SUPPLIES</th>
                <th>PHILHEALTH</th>
                <th>DISCOUNT</th>
                <th>GROSS</th>
                <th>NET</th> 
                <tr>
              </thead>
                  <tbody>

                     <?php
                            $x = 1;
                            $billquery = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
                            foreach ($billquery->result() as $data) {
                                echo '<tr>';
                                echo '<td>'.$x++.'</td>';
                                echo '<td>'.$data->patientname.'</td>';
                                echo '<td>'.$data->session_id.'</td>';
                                 //SERVICES
                                      $servicesquery = $this->db->get('services');
                                foreach ($servicesquery->result() as $service) {                                     

                                      $this->db->where('billing_serviceid', $service->service_id);
                                      $this->db->where('billing_patientid', $data->patient_id);
                                      $this->db->where('billing_id', $data->billing_id);
                                      $this->db->select_sum('billing_amount');
                                      $billing = $this->db->get('billing_entry');
                                      foreach ($billing->result() as $bill) {
                                        echo '<td>'.$bill->billing_amount.'</td>';
                                      }
                                }
                                  //MEDICAL SUPPLIES
                                      $this->db->where('billing_id', $data->billing_id);
                                      $this->db->where('billing_servicename', 'MEDICAL SUPPLIES');
                                      $this->db->select_sum('billing_amount');
                                      $medsup = $this->db->get('billing_entry');
                                      foreach ($medsup->result() as $med) {
                                        echo '<td>'.$med->billing_amount.'</td>';
                                      }

                                  //PHILHEALTH    
                                      $this->db->where('billing_id', $data->billing_id);                                          
                                      $this->db->select_sum('billing_philhealth');
                                      $philhealth = $this->db->get('billing_entry');
                                      foreach ($philhealth->result() as $phil) {
                                        echo '<td class="bg-info">'.$phil->billing_philhealth.'</td>';
                                      }

                                  //DISCOUNT
                                      $this->db->where('billing_id', $data->billing_id);                                          
                                      $this->db->select_sum('billing_discount');
                                      $philhealth = $this->db->get('billing_entry');
                                      foreach ($philhealth->result() as $phil) {
                                        echo '<td class="bg-info">'.$phil->billing_discount.'</td>';
                                      }

                                  //GROSS
                                      $this->db->where('billing_id', $data->billing_id);                                          
                                      $this->db->select_sum('billing_amount');
                                      $gross = $this->db->get('billing_entry');
                                      foreach ($gross->result() as $gr) {
                                        echo '<td class="bg-warning">'.$gr->billing_amount.'</td>';
                                      }



                                  echo '<td class="bg-danger">'.$data->total_amount.'</td>';
                                echo '</tr>';
                            }






                            echo '<tr>';
                            echo '<td colspan="3" style="text-align:right"><strong>TOTAL:</strong></td>';


                            //PROBLEM STARTS HERE
                            //TOTAL PER SERVICES

                                $quer = $this->db->get('services');
                                foreach ($quer->result() as $service) {                                                              
                                      $totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');                                                                                    
                                      foreach ($totserv->result() as $servdata) {
                                      $id = $servdata->id;   

                                            $this->db->where('billing_id', $servdata->billing_id);  
                                            $this->db->where('billing_serviceid', $service->service_id);                                   
                                            $this->db->select_sum('billing_amount');
                                            $medsup = $this->db->get('billing_entry');
                                            foreach ($medsup->result() as $med) {
                                              echo '<td class="bg-success">'.$med->billing_amount.'</td>';
                                            }

                                      }

                            }

                            //PROBLEM ENDS HERE





                            //TOTAL NET
                            $totalamt = $this->db->query('SELECT SUM(total_amount) AS totalamount FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
                            foreach ($totalamt->result() as $data) {
                              echo '<td>'.$data->totalamount.'</td>';
                            }
                            echo '</tr>';

                     ?>       

If you look at your image, it's clear to see you're outputting each value from the columns (in the green box) in your final row (350, zero, 350, 485, 485, 485, zero, 15, 300)

You can fix this by adding a placeholder variable to calculate the total for each service, e.g.:

foreach ($quer->result() as $service) {   // you want to output one <td> per service, so the <td> should be printed at the end of this foreach
  $serviceTotal = 0; // initialize the total for each service                                                          
  $totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');                                                                                    
  foreach ($totserv->result() as $servdata) {
  $id = $servdata->id;   

      $this->db->where('billing_id', $servdata->billing_id);  
      $this->db->where('billing_serviceid', $service->service_id);                                   
      $this->db->select_sum('billing_amount');
      $medsup = $this->db->get('billing_entry');
      foreach ($medsup->result() as $med) {
          $serviceTotal = $serviceTotal + $med->billing_amount     
      }
  }
  echo '<td class="bg-success">'. $serviceTotal .'</td>';
}

This way you will get one per service, and the $serviceTotal variable will contain the sum of each individual line item for that particular service.

You have more efficient options open to you as well - firstly, you could calculate the individual totals when you are getting them in the services code block, e.g.:

$serviceTotals = array(); // a holder for all the totals
foreach ($servicesquery->result() as $service) {                                     
    $this->db->where('billing_serviceid', $service->service_id);
    $this->db->where('billing_patientid', $data->patient_id);
    $this->db->where('billing_id', $data->billing_id);
    $this->db->select_sum('billing_amount');
    $billing = $this->db->get('billing_entry');
    foreach ($billing->result() as $bill) {
      $serviceTotals[$service->service_id] = $serviceTotals[$service->service_id] + $bill->billing_amount; //this updates the total for each service ID
      echo '<td>'.$bill->billing_amount.'</td>';
    }
}

You could then loop through this array to output each total.

Ideally, you should also consider calculating all these values in your controller, which would call the queries you need from your model(s), and then packaging that data in your $data variable. The controller can then pass all of that to your view. CodeIgniter has a lot of good resources for explaining the Model-View-Controller (MVC) pattern (it's a bit outside of the scope of this question to go into it here), and using CI without implementing the MVC pattern is going to make it harder to make use of most of the documentation and support for CI on the net.