How can I get data from two tables in Mysql and Codeigniter - php

I am working on a point of sale for a hardware shop developed using codeigniter framework.
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
Sales
Payments

You can reuse the given methods getSales() and getPayments() in a new method getSalesWithPayments() to combine the two results:
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. This way you can easily access any sale by its id without an expensive search.
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).
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. You can use MySQL Join to get combined data from 2 tables.

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();

Related

How to merge tabular data in PHP

I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }

Mysql returns duplicate in html table

I am trying to populate html table with data from mysql.But i am stuck on this part where each time i add some data it keeps repeating. On the picture below you can see that Test 1 repeat each time for every P20,P21,P24,P22,P23 and i needed to be one TEST1 for all of them. When i add Test 2 with value 19000 its making new P20 and all data come from Test 1 to Test 2. Can someone help me how to fix this.Any hint or suggestion wil be appreciated. Thank you all very much (Sorry for my bad english)
This is code that runs this
$sql = 'SELECT DISTINCT Pers.naam, Rol.funkcija,pdata.broj
FROM ids
left JOIN Pers ON ids.persid = Pers.id
left JOIN Rol ON ids.rolid = Rol.id
left JOIN pdata ON ids.pdataid = pdata.id
';
$query = $conn->prepare($sql);
$query->execute();
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php
foreach ($testing as $test):
?>
<th>
<?php
echo $test['naam'] . '<br />';
?>
</th>
<?php
endforeach;
?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($testing as $test): ?>
<tr>
<td><?php echo $test['funkcija']; ?></td>
<?php endforeach; ?>
<?php
foreach ($testing as $test):
?>
<td><?php echo $test['broj']; ?></td>
<?php
endforeach;
?>
</tr>
</table>
I would like to have it like this
There is some refactoring required to get to the output you want.
You seem to be enumerating quite a lot of data from your SQL result, so there is some grouping required to make things easier.
To get the appropriate number to each "naam" and "funkcija" you can use array_filter which however could theoretically return several numbers in each case.
You'll also have to nest a couple foreach instead of running them after one another.
If I understand your data structure correctly, this should at least give you a good starting point for the output you want:
<?php
// ...
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
$naams = array_unique(array_column($testing, "naam"));
$funkcijas = array_unique(array_column($testing, "funkcija"));
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php foreach ($naams as $naam): ?>
<th>
<?php echo $naam; ?>
</th>
<?php endforeach; ?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($funkcijas as $funkcija): ?>
<tr>
<td><?php echo $funkcija; ?></td>
<?php foreach ($naams as $naam): ?>
<?php
$data = array_filter(
$testing,
function ($v) use ($naam, $funkcija)
{
return $v["naam"] === $naam && $v["funkcija"] === $funkcija;
}
);
foreach ($data as $value): ?>
<td><?php echo $value["broj"]; ?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

SEARCHING with some condition in codeigniter

i have a web project using codeigniter.
i have a problem with searching in my project. i have to show the multiple result from searching page with some keyword.
here is my Model
function find_user($like){
$this->db->from('user');
$this->db->like('name', $like,'after');
$result =$this->db->get();
return $result->result_array();
}
and in my user table, include
id | name | place_code
in the user table , column place_code is use to show the place from the user
here is my Controller
function search(){
$query = $this->input->post('query_cari');
$find = $this->m_user->find_user($query);
foreach ($find as $key) {
$code = $key['place_code'];
if ($code == '1') {
$place = 'Secretray';
}elseif($code == '2'){
$place = 'OB';
}elseif($code == '3'){
$place ='Manager';
}
}
$data['result'] = $find;
$data['place'] = $place;
$this->load->view('home/search',$data);
}
that's my code for controller, include a logic for get the position from user in office. but the problem is, when i get a result just 1 result, the place is right. but if i get more than 1 result, place is going wrong and just show the place for all result is the place from the last result in searching.
what i want is, all result just shown their own place.
here my VIEW
<?php foreach ($find as $key: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $key['id'] ?></td>
<td><?php echo $key['name'] ?></td>
<td><?php echo $place ?></td>
</tr>
</tbody>
</table>
<?php endforeach ?>
It is normal that you always get the last place because your logic is not correct. First, you have an error in your model. Secondly, you can improve your model code:
function find_user($like)
{
$this->db->like('name', $like, 'after');
return $this->db->get('user')->result();
}
Now, in your controller you want to change the variable place according to the value of place_code. You are allowed to add a new key (or change an existing one) in real time to the stdClass() as follows:
foreach($find as $i => $result)
{
// By default, we assume the 'place_code' is equal to '1'
$find[$i]->place = 'Secretray';
if($result->place_code == '2')
$find[$i]->place = 'OB';
else
$find[$i]->place = 'Manager';
}
$data['find'] = $find;
$this->load->view('home/search', $data);
Finally, in your view:
<?php foreach ($find as $result){ ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
<td><?php echo $result->place; ?></td>
</tr>
</tbody>
</table>
<?php } ?>

Call function throught another file

View/Invoice/View.ctp
<?php
if ( $payments == NULL ) {
echo '<tr><td> -- No Payments Are Associated With This Invoice -- </td>
<td></td>
<td></td></tr>';
}
?>
<? foreach ($payments as $payment): ?>
<tr id="payment_row_<?= $payment['id'] ?>">
<td class="responsive_cell"><?php echo $payment['method']?></td>
<td class="date_cell responsive_cell"><?php echo $payment['date']?></td>
<td class="right responsive_cell"><?php echo number_format($payment['amount'], 2, '.', ',')?></td>
</tr>
<?php $paymentsTotal += $payment['amount'] ?>
<? endforeach; ?>
View/Invoice/email.ctp
$bedrag = $settings['Setting']['currency'].$thisInvoice['Payment'];
This echo's €Array in EMail.ctp and in View.ctp show the right amount.
How do i get the right amount in Email.ctp ?

How can i sum dynamic array

My code is below
<?php
foreach($query2->result() as $row2)
{
$sub_head_id = $row2->sub_head_id;
?>
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
$farm_id_inner = $row3->farm_id;
?>
<td>
<?php
$s_h_e_amount = $this->report_model->get_amount($farm_id_inner,$head_id,$sub_head_id);
?>
</td>
<?php
}
?>
<td>
<?php
$total += $s_h_e_amount;
?>
</td>
</tr>
<?php
}
}
?>
Basically i want to know how can i solve it,
<tr>
<?php
$query3 = $this->db->select('farm_id')->get('spf_farm_info'); $she_am_total = 0;
foreach($query3->result() as $row3)
{
?>
<td>
<?php
echo "Total :";
echo $total;
?>
</td>
<?php
}
?>
<td>
Grand Total:
</td>
</tr>
Here, Table column will increase dynamically, and will get different value every td(may be 2/3/4/..) from get_amount function.
Now i want to sum every tr's value according to column.
here it will be sum dynamically which will be show.
Please help me how can i solve properly
Simple SOlution is Take Four Variables
$Cola_total=0;
$Colb_total=0;
$Colc_total=0;
$Cold_total=0;
in your loop sum these Columns
foreach($array as $val){
echo "<tr><td>$val[0]</td> <td>$val[1]</td> <td>$val[2]</td><td>$val[3]</td></tr>";
$Cola_total=+$val[0];
$Colb_total=+$val[1];
$Colc_total=+$val[2];
$Cold_total=+$val[3];
}
and after the loop put the sum in last row
echo "<tr><td>$Cola_total</td> <td>$Colb_total</td> <td>$Colc_total</td><td>$Cold_total</td></tr>";
it may give you some idea
Here , since using Codeigniter, it will be used that select_sum(). then will get proper result.

Categories