[英]How can I get data from two tables in Mysql and Codeigniter
I am working on a point of sale for a hardware shop developed using codeigniter framework. 我正在为使用codeigniter框架开发的硬件商店开发销售点。
I want to come up with a detailed statement of account like in the picture below 我想提出一份详细的账户说明,如下图所示
I have two tables which are sales and payment, I want to get data from this two tables so that I can generate a statement of account of a specific customer. 我有两个表是销售和付款,我想从这两个表中获取数据,以便我可以生成特定客户的帐户报表。
This will help since the customer can be able to see all the items he/she bought on cash basis or credit basis and also show how much they has paid grouped by date. 这将有所帮助,因为客户能够以现金或信用为基础查看他/她购买的所有物品,并且还显示他们按日期分组的付款额。
It will also be possible to calculate the amount due. 还可以计算到期金额。
I can be able to list separately (sales and payment) by using the below code and thereby calculate the amount due. 我可以使用以下代码单独列出(销售和付款),从而计算到期金额。
Sales 销售
<?php $salestotal = 0; if (is_array($sales) || is_object($sales)) { foreach ($sales as $key=>$sale): {?> <tr> <td> <?php echo $sale->date; ?> </td> <td> <?php echo $sale->id; ?> </td> <td> <?php echo $sale->grand_total; ?> </td> <td> <?php echo $sale->paid; ?> </td> <td></td> </tr> <?php if(isset($sale->grand_total)) $salestotal += $sale->grand_total; } endforeach ; }?>
Payments 支付
<?php $paymentstotal = 0; if (is_array($payments) || is_object($payments)) { foreach ($payments as $key=>$payment): {?> <tr> <td> <?php echo $payment->date; ?> </td> <td class="text-center"> <?php echo $payment->sale_id; ?> </td> <td class="text-center"> <?php echo $payment->paid_by; ?> </td> <td class="text-center"> <?php echo $payment->cheque_no; ?> </td> <td class="text-center"> <?php echo $payment->amount; ?> </td> <td class="text-center"> <?php echo $this->customers_model->getUserna($payment->created_by); ?> </td> <td class="text-center"> <?php echo $payment->pos_paid; ?> </td> <td class="text-center"> <?php echo $payment->pos_balance; ?> </td> <td class="text-right"> <?php echo $this->customers_model->getStorename($payment->store_id); ?> </td> </tr> <?php if(isset($payment->amount)) $paymentstotal += $payment->amount; } endforeach ;}?>
My controller 我的控制器
function statement($id = NULL) { if (!$this->Admin) { $this->session->set_flashdata('error', $this->lang->line('access_denied')); redirect('pos'); } if($this->input->get('id')) { $id = $this->input->get('id', TRUE); } $this->data['sales'] = $this->customers_model->getSales($id); $this->data['payments'] = $this->customers_model->getPayments($id); $this->data['customer'] = $this->customers_model->getCustomername($id); $this->data['customer_id'] = $id; $this->page_cons('customers/statement', $this->data); }
My Model 我的模特
public function getSales($id) { $q = $this->db->get_where('sales', array('customer_id' => $id)); if( $q->num_rows() > 0 ) { return $q->result(); } return FALSE; } public function getPayments($id) { $q = $this->db->get_where('payments', array('customer_id' => $id)); if( $q->num_rows() > 0 ) { return $q->result(); } return FALSE; }
How do I combine this two tables? 我如何组合这两个表?
I hope I am clear enough on this question, I have been trying to Google but I have no luck. 我希望我对这个问题很清楚,我一直在尝试谷歌,但我没有运气。
I will appreciate any help. 我将不胜感激任何帮助。 Thanks 谢谢
Edit 编辑
MYSQL tables MYSQL表
Payments 支付
You can reuse the given methods getSales()
and getPayments()
in a new method getSalesWithPayments()
to combine the two results: 您可以在新方法getSalesWithPayments()
重用给定方法getSales()
和getPayments()
getSalesWithPayments()
来组合两个结果:
public function getSalesWithPayments($customerId) {
$sales = [];
foreach ($this->getSales($customerId) as $sale) {
$sale->payment = null;
$sales[$sale->id] = $sale;
}
foreach ($this->getPayments($customerId) as $payment) {
$sales[$payment->sale_id]->payment = $payment;
}
return $sales;
}
In the first loop you initialize $sale->payment
(for the case that a sale has no payment yet) and populate the $sales
array, which is indexed by the id
column. 在第一个循环中,您初始化$sale->payment
(对于销售尚未付款的情况)并填充$sales
数组,该数组由id
列索引。 This way you can easily access any sale by its id without an expensive search. 通过这种方式,您无需昂贵的搜索即可轻松访问其ID。
In the second loop you assign the payments to the corresponding sales. 在第二个循环中,您将付款分配给相应的销售。
Note that you might need to add logic to handle empty results (which wouldn't be necessary if you would just return an empty array instead of FALSE
). 请注意,您可能需要添加逻辑来处理空结果(如果您只返回一个空数组而不是FALSE
不需要)。
You can now use it like.. 你现在可以像...一样使用它
Controller: 控制器:
$this->data['sales'] = $this->customers_model->getSalesWithPayments($id);
View: 视图:
<?php foreach ($sales as $sale): ?>
<tr>
<td><?php echo $sale->date; ?></td>
<td><?php echo $sale->id; ?></td>
...
<?php if($sale->payment !== null): ?>
<td><?php echo $sale->payment->date; ?></td>
<td><?php echo $sale->payment->amount; ?></td>
...
<?php else: // empty cells if no payment ?>
<td></td>
<td></td>
...
<?php endif; ?>
</tr>
<?php endforeach; ?>
If you post the sales
and payments
table the answer will be better and exact. 如果您发布sales
和payments
表,答案将更好,更准确。 You can use MySQL Join to get combined data from 2 tables. 您可以使用MySQL Join从2个表中获取组合数据。
Try this: 尝试这个:
$this->db->select('*')
->from('sales')
->join('payments', 'payments.sales_id = sales.id', 'right')
->where(array('sales.customer_id' => $id));
$results = $this->db->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.