我目前有问题,我的总数。那么,我有3个不同的表格。 billing_payments,billing_entry和services。一张表中的几列总和
我的问题是,我不能得到每个SERVICES的总和。我提供了一个截图,以便你明白我的意思。
以下是报告的代码。我添加了一些注释来指出问题的起止点。
<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>';
?>
我想你不能使用循环来获得'sum',因为'sum'将是列数据的总和。你可以尝试'$ bill-> row() - > billing_amount;' –
请学习使用MODELS这样的内联查询是非常糟糕的做法.. 您可以使它更容易1服务模型有几个方法返回服务和数量.. – Svetoslav