Incorrect output when calculating totals - php

I have a web page where i am able to export a .csv file that is readable on excel. The invoice pulls the data from my database to calculate the total and grand total using the following columns:
quantity, packing_price, courier_price
I have noticed that my code doesn't output the correct answer when the prices contains a '£' sound in front of it. Is there a way that i could make this work for both number and currency data types?
CODE:
$output2= "";
$sql2 = mysql_query("
SELECT j.date
, j.order_ref
, j.quantity
, j.packing_price
, j.dispatch_type
, j.courier_price
FROM Jobs j
WHERE j.order_type = 'Retail International'
AND j.confirmed = 'Yes';
");
$columns_total2 = mysql_num_fields($sql2);
$total3 = "Total";
for ($i = 0; $i < $columns_total2; $i++)
{
$heading2 = mysql_field_name($sql2, $i);
$output2 .= '"'.$heading2.'",';
}
$output2 .= $total3;
$output2 .="\n";
$sum2 = 0;
while ($row = mysql_fetch_array($sql2)) {
for ($i = 0; $i < $columns_total2; $i++) {
$output2 .='"'.$row["$i"].'",';
}
$qty2 = $row['quantity'];
$pack_price2 = $row['packing_price'];
$dispatch2 = $row['courier_price'];
$total2 = (($qty2*$pack_price2) + $dispatch2);
$total3 = $total2*1.2;
$output2 .= $total3;
$sum2 += $total3; // Add to overall total
$output2 .="\n";
}
Output:
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-07-03at113133_zpsbcc09900.png

Use this Code....
$output= "<table border='1' width='60%'><tr>";
$sql = " SELECT j.date ,
j.order_ref ,
j.quantity ,
j.packing_price ,
j.dispatch_type ,
j.courier_price
FROM Jobs j
WHERE j.order_type = 'Retail International'
AND j.confirmed = 'Yes' ";
$query = mysql_query($sql);
$total_columns = mysql_num_fields($query);
for ($i = 0; $i < $total_columns; $i++){
$heading = mysql_field_name($query, $i);
$output .= '<td>' . $heading . '</td>';
if(($i+1) == $total_columns) $output .= '<td>Total</td></tr>';
}
while ($row = mysql_fetch_array($query)) {
$total_price = 0;
$total_price =( ( $row['quantity'] * $row['packing_price'] ) +
$row['courier_price'] );
$total_price = $total_price * 1.2;
$timestamp = DateTime::createFromFormat('Y-m-d h:i:s',
$row['date'])->getTimestamp();
$output .= '<tr>';
$output .= '<td>' . date("d/m/Y", $timestamp) . '</td>';
$output .= '<tr>';
$output .= '<td>' . date("d/m/Y", strtotime($row['date']) . '</td>';
$output .= '<td>' . $row['order_ref'] . '</td>';
$output .= '<td>' . $row['quantity']. '</td>';
$output .= '<td>' . $row['packing_price'] . '</td>';
$output .= '<td>' . $row['dispatch_type'] . '</td>';
$output .= '<td>' . $row['courier_price'] . '</td>';
$output .= '<td>' . number_format($total_price, 2) . '</td>';
$output .= '</tr>';
}
$output .= "</table>";
echo '<br>' . $output;

Related

How to customize pagination in laravel to show a number of elements for each page

my application basically generate reports for some users
I fetch the rows from the database
every row contains a 'name' and 'work time' and others...
every user has multiple rows with the same name but a different work time value
I calculated the total of the 'work time' in the blade depending on the next row if its the same name I 'll keep adding work time, if the next row is for another user then I will show the total for the current user and start calculating the total for the next user
my problem is with pagination:
at the end of each page, the rows of the same user do not appear all of it
and my blade logic calculate only what appears on that page (see the picture)
notice here that other rows with the same name exist in the second page
and their total calculated separately
is there is any way to tell the paginator to devise rows on a condition besides that number ->paginate(15+condition) because I want all the rows for a specific user to stay on the same page.
this is the query:
$shifts = DB::table('driver_shifts')
->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
->where ('driver_shifts.shift_start_time','>',$startDate)
->where ('driver_shifts.shift_start_time','<',$endDate)
->orderby('drivers.LastName')
->orderby('driver_shifts.shift_start_time')
->paginate(20)
->appends(request()->query());
this is the blade logic inside a table. Ps: I know it not a good practice but I coudn't find a better way to do it in the controller:
$total_time = 0.00;
$break_time = 0.00;
$work_time = 0.00;
for ($i = 0; $i < count($shifts); $i++)
{
$break_time_item = $shifts[$i]->total_time - $shifts[$i]->work_time;
echo '<tr>';
echo '<td>' . $shifts[$i]->FirstName . ' ' . $shifts[$i]->LastName . '</td>';
echo '<td>' . $shifts[$i]->vehicle_id . '</td>';
echo '<td>' . $shifts[$i]->shift_start_time . '</td>';
echo '<td>' . $shifts[$i]->shift_end_time . '</td>';
echo '<td>' . $shifts[$i]->total_time . '</td>';
echo '<td>' . $break_time_item . '</td>';
echo '<td>' . $shifts[$i]->work_time . '</td>';
echo '</tr>';
//calculation totals for the current driver
//dd($shifts[$i]->break_time);
$total_time = $total_time + $shifts[$i]->total_time;
$work_time = $work_time + $shifts[$i]->work_time;
if (isset($shifts[$i + 1]))
{
if ($shifts[$i]->LastName !== $shifts[$i + 1]->LastName)
{
//calculating break time
$break_time = $total_time - $work_time;
//converting minutes to houres
$total_time_hours = floor($total_time / 60);
$total_time_min = $total_time - ($total_time_hours * 60);
$work_time_hours = floor($work_time / 60);
$work_time_min = $work_time - ($work_time_hours * 60);
$break_time_hours = floor($break_time / 60);
$break_time_min = $break_time - ($break_time_hours * 60);
echo '<tr class="table-primary text-uppercase ">';
echo '<td colspan="4">' . $shifts[$i]->FirstName . ' ' . $shifts[$i]->LastName . ' TOTAL ' . '</td>';
if ($total_time_hours == 0 && $total_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $total_time_hours . "h:" . $total_time_min . "min" . '</td>';
}
if ($break_time_hours == 0 && $break_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $break_time_hours . "h:" . $break_time_min . "min" . '</td>';
}
if ($work_time_hours == 0 && $work_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $work_time_hours . "h:" . $work_time_min . "min" . '</td>';
}
echo '</tr>';
$total_time = 0;
$break_time = 0;
$work_time = 0;
}
}

How to calculate cumulative sum in PHP?

I want to calculate the sum and output a two columns table. But, the format is wrong. The numbers are all packed in one line.
<td>
<?php
for ($j = 1; $j <= 10; $j++)
{
print "<tr>";
print $j;
print "</tr>";
}
?>
</td>
<td>
<?php
$sum = 0;
for($i = 1; $i<=10; $i++)
{
print "<tr>";
print $sum = $sum + $i;
print "</tr>";
}
?>
</td>
Here, we would be writing one or two for loops, join our desired HTML string, and finally we would print it.
Test:
<?php
$html = '<tr>';
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
print($html);
?>
We would be also adding table open and close tags (<table>, </table>):
<?php
$html = '<table>';
$html .= '<tr>';
$html .= "<td>Number</td>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$html .= "<td>Cumulative Sum</td>";
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
$html .= '</table>';
print($html);
?>
For two columns, we would just use one for` loop:
<?php
$html = '<table>';
$html .= "<tr><th>Number</th><th>Cumulative Sum</th></tr>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<tr>";
$html .= "<th>" . $j . "</th>";
$sum += $j;
$html .= "<th>" . $sum . "</th>";
$html .= "</tr>";
}
$html .= '</table>';
print($html);
I ended up here when looking to get a cumulative sum in PHP. For the ones like me, I wrote the following function. It's safe for empty arrays as well.
function cumsum ($arr) {
return array_reduce($arr, function ($c, $i) { $c[] = end($c) + $i; return $c; }, []);
}

Selecting multiple tables only working in one table

I'm trying to connect four tables. order_history, orders_list, infistall_order, and delivery_orders. When I run the code into the table of order_history, the results are as what I am expecting. But when I run the code into the webpages, it only selects the value from the table of order_history.
Note: What I mean with running the code into the table of order_history is when I enter phpMyAdmin and go to the order_history table and run SQL there with the SQL code I have.
<?php
include ("config.php");
$results = $mysqli->query
("
SELECT orders_history.transaction_id,
orders_history.items,
orders_history.quantity,
orders_history.one_product_price,
orders_list.status,
orders_list.invoices,
orders_list.payment_method,
orders_list.order_method,
infistall_order.address,
delivery_orders.address,
delivery_orders.service,
delivery_orders.cost,
delivery_orders.city
FROM orders_history
LEFT JOIN orders_list
ON orders_history.transaction_id = orders_list.transaction_id
LEFT JOIN infistall_order
ON orders_history.transaction_id = infistall_order.transaction_id
LEFT JOIN delivery_orders
ON orders_history.transaction_id = delivery_orders.transaction_id
WHERE orders_list.customer_name = 'Klaudia'"
);
Actually I am trying to collect information from all column in all table based on the transaction_id.
$orders = array();
$html = '';
if ($results) {
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array('quantity' => $obj->quantity, 'invoices' => $obj->one_product_price);
}
$html .= '<table width="70%"><tr>';
$html .= '<td>items</td>';
$html .= '<td>quantity</td>';
$html .= '<td>one_product_price</td>';
$html .= '<td>status</td>';
$html .= '<td>invoices</td>';
$html .= '<td>payment_method</td>';
$html .= '<td>order_method</td>';
$html .= '<td>address</td>';
$html .= '<td>service</td>';
$html .= '<td>cost</td>';
$html .= '<td>city</td></tr>';
foreach ($orders AS $order_id => $order) {
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['items'] . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$html .= '<td>' . $data['status'] . '</td>';
$html .= '<td>' . $data['invoices'] . '</td>';
$html .= '<td>' . $data['payment_method'] . '</td>';
$html .= '<td>' . $data['order_method'] . '</td>';
$html .= '<td>' . $data['address'] . '</td>';
$html .= '<td>' . $data['service'] . '</td>';
$html .= '<td>' . $data['cost'] . '</td>';
$html .= '<td>' . $data['city'] . '</td>';
$row++;
}
$html .= '</tr><tbody>';
}
$html .= '</table>';
}
echo $html;
?>
On your loop:
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array(
'quantity' => $obj->quantity,
'invoices' => $obj->one_product_price
);
}
You can change it to:
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items][] = array(
'quantity' => $obj->quantity,
'invoices' => $obj->one_product_price
);
}
It will prevent the data from replacing one to another, the [] sign on array is make the array to become dynamic, the key will be count from 0 and will be increment by 1 as you add more data into that array.

Prevent one data to loop

<?php
include ("config.php");
$results = $mysqli->query
("
SELECT orders_history.transaction_id,
orders_history.items,
orders_history.quantity,
orders_history.one_product_price,
orders_list.status,
orders_list.invoices,
orders_list.payment_method,
orders_list.order_method,
delivery_orders.address,
delivery_orders.service,
delivery_orders.cost,
delivery_orders.city
FROM orders_history
LEFT JOIN orders_list
ON orders_history.transaction_id = orders_list.transaction_id
LEFT JOIN infistall_order
ON orders_history.transaction_id = infistall_order.transaction_id
LEFT JOIN delivery_orders
ON orders_history.transaction_id = delivery_orders.transaction_id
WHERE orders_list.customer_name = 'Klaudia'"
);
$orders = array();
$html = '';
if ($results) {
while($obj = $results->fetch_object()) {
$orders[$obj->transaction_id][$obj->items] = array(
'invoices' => $obj->invoices,
'status' => $obj->status,
'payment_method' => $obj->payment_method,
'service' => $obj->service,
'cost' => $obj->cost,
'quantity' => $obj->quantity,
'one_product_price' => $obj->one_product_price,
'city' => $obj->city);
}
$html .= '<table width="70%"><tr>';
$html .= '<td>transaction_id</td>';
$html .= '<td>items</td>';
$html .= '<td>quantity</td>';
$html .= '<td>one_product_price</td>';
$html .= '<td>status</td>';
foreach ($orders AS $order_id => $order) {
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$row++;
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
}
$html .= '</tr><tbody>';
}
$html .= '</table>';
}
echo $html;
?>
The code above will result like this:
-----------------------------------------------------------------------
id_cart products quantity invoices status
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0001 that 1 $20 pending
-------------------------------------------------------
those 2 $20 pending pending
-----------------------------------------------------------------------
Total Invoices: $60
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0002 that 1 $20 approved
-------------------------------------------------------
those 2 $20 approved approved
-----------------------------------------------------------------------
Total Invoices: $60
Have a look at the column of status, where the result is looping.
What I want is it shouldn't be looping and only get one result as how the column of id_cart looks like. This is the code for the column of status
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
I need to solve it!
[EDIT]
When I move the code like this:
foreach ($orders AS $order_id => $order) {
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
$row++;
}
$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>';
$html .= '</tr><tbody>';
-----------------------------------------------------------------------
id_cart products quantity invoices status
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0001 that 1 $20
-------------------------------------------------------
those 2 $20 pending
-----------------------------------------------------------------------
Total Invoices: $60
-----------------------------------------------------------------------
this 2 $20
-------------------------------------------------------
0002 that 1 $20
-------------------------------------------------------
those 2 $20 approved
-----------------------------------------------------------------------
Total Invoices: $60
The resultin the column of status in at the very bottom of the tbody. I want it as how the column of id_cart is.
You need 1 value of status per tbody, so put it outside your loop. Also, because you're reusing $order, I've put count($order) in a seperate variable. Also, close your </tbody> tag.
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . $orderCount . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
if ($row == 1)
$html .= '<td rowspan="' . $orderCount . '">' . $data['status'] . '</td>';
$row++;
}
$html .= '</tr></tbody>';
}
Use valign attribute for td element to get the value aligned center vertically.
$html .= '<td rowspan="' . $orderCount . '" valign="middle">' . $data['status'] . '</td>';
Hope it will work for you.
Check this out:
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$html .= '<td>' . $data['quantity'] . '</td>';
$html .= '<td>' . $data['one_product_price'] . '</td>';
if( $row == 1 )
{
$html .= '<td style="vertical-align: middle" rowspan="' . count($order) . '">'. $data['status'] . '</td>';
}
$row++;
}

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )

Categories