enter image description here*now how can i calculate the sub total of the (tolal) field values these values are not from database, these are from (price of item * quantity)*
This is my code:
<?php
$ipAdd = getRealIpAddr();
$checkPrice= "select * from cart where ipAdd= '$ipAdd' ";
$run= mysqli_query($conn,$checkPrice);
while($record= mysqli_fetch_array($run)){
$proId= $record['pId'];
$cId= $record['cId'];
$cQuant= $record['qnty'];
$proPric= "select * from products where prodId= '$proId' ";
$runPrice=mysqli_query($conn, $proPric);
while($pPrice=mysqli_fetch_array($runPrice)){
$proPri= $pPrice['prodPrice'];
$t = $proPri* $cQuant ;
}
}
?>
Alrighty, so assuming you want total of all items times all the quantities, here's what you do:
$ipAdd = getRealIpAddr();
$total = 0;
$checkPrice = "SELECT * FROM cart WHERE ipAdd = '$ipAdd' ";
$run = mysqli_query($conn,$checkPrice);
while($record = mysqli_fetch_array($run)){
$proId = $record['pId'];
$cId = $record['cId'];
$cQuant = $record['qnty'];
$proPric = "SELECT * FROM products WHERE prodId = '$proId' ";
$runPrice = mysqli_query($conn, $proPric);
$pPrice = mysqli_fetch_array($runPrice);
$proPri = $pPrice['prodPrice'];
$t = $proPri * $cQuant ;
$total += $t;
}
echo $total; // will be a sum of all prices times all quantities
You don't need while if you're fetching just one value, literally just removing it works fine
You start this process by declaring $total variable, in this case resetting it
You do everything you already did, after which you increment $total by whatever is the value of $t
Keep in mind that $total will change during this while loop, so considering this looks like a per-user-query, make sure to use it outside of the loop.
for($i=0; $i<count($_POST['order_item']); $i++)
{
$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($billed_qty > 0)
{
$batchout = 0;
$rem = max($row['total_qty']-$billed_qty,0);
if($rem == 0)
$batchout = $row['total_qty']; //This means there are no items of this cost remaining
else
$batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
$billed_qty -= $batchout;
$qtyout += $batchout;
$sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
$dbo->query($sql);
$sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
$dbo->query($sql1);
}
}
$sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$product_name."','".$customer_name."','".$salesman_name."','".$order_item."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$_POST['itqty'][$i]."','".$batch_no."','".$prod_mrps."','".$prices."','".$discount."','".$amount."')";
$dbo->query($sql);
}
This is my code to update inventory with batch and invoice table. This code is only working for my first product. Need this to be done for all products of an order. Any help is appreciated.
for($i=0; $i<count($_POST['order_item']); $i++)
{
$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
if($billed_qty > 0)
{
$batchout = 0;
$rem = max($row['total_qty']-$billed_qty,0);
if($rem == 0)
$batchout = $row['total_qty']; //This means there are no items of this cost remaining
else
$batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
$billed_qty -= $batchout;
$qtyout += $batchout;
$sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
$dbo->query($sql);
$sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
$dbo->query($sql1);
$sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$_POST['product_name'][$i]."','".$customer_name."','".$salesman_name."','".$_POST['order_item'][$i]."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$batchout."','".$row["batch_no"]."','".$_POST['mrps'][$i]."','".$_POST['price'][$i]."','".$_POST['qty'][$i]."','".$_POST['total'][$i]."')";
$dbo->query($sql);
}
}
}
Found the answer after lot of debugging.
Thanks for all your response.
pulling hair out now
I have a query that counts all relevant $price values in the array
Basically the initial query checks the table to jobs that are completed but not invoiced
The second query ( inside the initial query loop ) gets all the items that need adding up ( these values are found inside another table ( workshop-items ) and are checked against the $item array values
the total is being calculated ok , i think it has something to do with where the $total is placed as its adding up ALL returned totals not the individual row totals
code below
<ul class="list-group">
<?php
$uninvoicedq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE completed = '1' AND invoiced = '0' AND wscid !='0' ORDER BY workstartdate ASC");
$uninvoiced = mysqli_fetch_assoc($uninvoicedq);
if($uninvoiced) {
do {
// User Query
$wscid = $uninvoiced['wscid'];
$userq = mysqli_query($cona,"SELECT * FROM `users` WHERE userid = '$wscid'");
$user = mysqli_fetch_assoc($userq);
$wtbdq = mysqli_query($con,"SELECT * FROM `workshop-jobs` WHERE wsjid = '$uninvoiced[wsjid]'");
$wtbdr = mysqli_fetch_assoc($wtbdq);
do {
$wtbd = explode(":",$wtbdr['worktobedone']);
foreach($wtbd as $item)
{
$priceq = mysqli_query($con,"SELECT * FROM `workshop-items` WHERE wsiid = '$item'");
$pricer = mysqli_fetch_assoc($priceq);
$price[] = $pricer['incvat'];
$items[] = $pricer['description'];
//echo $item.' - '. $pricer['incvat'].'<br>';
}
$total = array_sum($price);
} while($wtbdr = mysqli_fetch_assoc($wtbdq));
?>
<li class="list-group-item text-right" style="border:none;" title="<?php echo $itemview;?>"><span class="badge pull-left" style="background-color:#F00;">Not Invoiced</span><?php echo '£'.$total.' - '; echo $user['forename'].' '.$user['surname'].' - ' .$uninvoiced['summary'];?> </li>
<?
$itemList = implode(":",$items);
$itemview = str_replace(":","\n",$itemList);
?>
<? } while($uninvoiced = mysqli_fetch_assoc($uninvoicedq));
} else {
echo "No Jobs Waiting To Invoiced";
}
?>
</ul>
If you mean that each row of the do while should be different totals, then, when do begins, set $price = []; or $price = array(); or to null, because you will have all the previous prices added up as a final price, if your final price is for each query of the do while and not for the hole, do what I say.
Remember to do $total += and not $total = because you will overwrite the variable that you are using outside the main loop, so you would get a wrong total.
I use a while loop to process table record like this:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
}
mysqli_free_result($result);
}
I have all my table records, and now I want to sum up all quantity fields , but I don't know how to do it.
For example, if I got 10 records quantity for records like 2, 1, 5, 1, 3, 6 etc, I would like to sum them like this: 2+1+5+1+3+6 = 18
If you can do something in mysql - do it. Use SUM aggregation function:
$get_s = "SELECT SUM(sells_quantity) as sells_sum FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$show_s = mysqli_fetch_assoc($result);
echo $show_s['sells_sum'];
}
mysqli_free_result($result);
Still, if you need certain values of rows - you can count sum in a loop:
$get_s = "SELECT * FROM sells ORDER BY sells_date";
if ($result = mysqli_query($AECONNECT, $get_s)) {
$total = 0;
while ($show_s = mysqli_fetch_assoc($result)) {
$quantity = $show_s['sells_quantity'];
$total += $quantity;
}
mysqli_free_result($result);
echo $total;
}
But mysql SUM() is preferrable.
I'm having an issue with with a PHP function displaying multiple sums instead of just one when I use array_sum. I'm using phpMyAdmin and the two database tables for this function. These are the tables:
games:
cart:
function total_price() {
$total = 0;
global $con;
$ip = getIP();
$select_price = "select * from cart where ip_add = '$ip'";
$run_price = mysqli_query ($con, $select_price);
while ($g_price = mysqli_fetch_array($run_price)) {
$Cart_Game_ID = $g_price['g_ID'];
$cart_price = "select * from games where Game_ID = '$Cart_Game_ID'";
$run_game_price = mysqli_query($con, $cart_price);
while($pp_price = mysqli_fetch_array($run_game_price)){
$Game_Price = array($pp_price['Game_Price']);
$values = array_sum($Game_Price);
$total += $values;
}
echo "$" . $total;
}
}
Every game in my database costs $20. There are currently 5 games in my database. When I run this function I get $20$40$60$80$100. All i need is the $100. Not the previous 4 sums.
Move this outside the While Loop:
echo "$" . $total;