2015-08-30 54 views
1

我目前有问题,我的总数。那么,我有3个不同的表格。 billing_payments,billing_entryservices一张表中的几列总和

我的问题是,我不能得到每个SERVICES的总和。我提供了一个截图,以便你明白我的意思。

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

        ?>  
+0

我想你不能使用循环来获得'sum',因为'sum'将是列数据的总和。你可以尝试'$ bill-> row() - > billing_amount;' –

+1

请学习使用MODELS这样的内联查询是非常糟糕的做法.. 您可以使它更容易1服务模型有几个方法返回服务和数量.. – Svetoslav

回答

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)模式(这里有一点不在这个问题的范围之内),而使用CI而不实现MVC模式会使它难以在网上使用CI的大部分文档和支持。