表中多列的总和

时间:2015-08-30 10:46:41

标签: php codeigniter

我目前在总计方面遇到问题。好吧,我有3张不同的桌子。 billing_payments billing_entry 服务

我的问题是,我无法获得每个服务的总数。我提供了一个截图,以便你明白我的意思。

enter image description here

以下是报告的代码。我添加了一些注释来指出问题的开始和结束位置。

<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>';

                     ?>       

1 个答案:

答案 0 :(得分:0)

如果您查看自己的图片,可以清楚地看到您从最后一行(350, zero, 350, 485, 485, 485, zero, 15, 300)的列(绿色框中)输出每个值

您可以通过添加占位符变量来计算每项服务的总数,例如:

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>';
}

通过这种方式,您将获得每项服务一项,$serviceTotal变量将包含该特定服务的每个订单项的总和。

您也可以使用更有效的选项 - 首先,您可以在服务代码块中获取个别总计时计算个别总数,例如:

$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>';
    }
}

然后,您可以遍历此数组以输出每个总数。

理想情况下,您还应该考虑在控制器中计算所有这些值,这些值可以从模型中调用所需的查询,然后将这些数据打包到$data变量中。然后控制器可以将所有这些传递给您的视图。 CodeIgniter有很多很好的资源来解释模型 - 视图 - 控制器(MVC)模式(这个问题超出了这个问题的范围),并且在没有实现MVC模式的情况下使用CI使网络上的CI大部分文档和支持变得更加困难。