Jump to content

[SOLVED] Need help tweaking code


Scooby08

Recommended Posts

Im trying to add up totals for each order and display that total.. The problem is that this code adds up all totals for all orders.. What would I need to do to display the total for each order_id??

 

<?php						
$query_total  = "SELECT dw_order_items.order_id, dw_order_items.item_id, dw_order_items.item_qty, dw_items.item_price ";
$query_total .= "FROM dw_order_items, dw_items ";
$query_total .= "WHERE dw_order_items.order_id = '$order_id' ";
$query_total .= "AND dw_order_items.item_id = dw_items.item_id ";
$result_total = mysql_query($query_total) or die(mysql_error());	
while($row_total = mysql_fetch_array($result_total)) {
$item_id = $row_total['item_id'];
$item_qty = $row_total['item_qty'];
$item_price = $row_total['item_price'];
$total_item_price += $item_qty * $item_price; // This is the line that adds all totals
}
echo show_price($total_item_price);	 
?>

I know that line I have marked keeps adding all totals, but I don't know what else to do??? Any ideas out there??

Link to comment
Share on other sites

This should do it:

 

$order_totals = array();
while($row_total = mysql_fetch_array($result_total)) {
$order_id = $row_total['order_id'];
        $item_id = $row_total['item_id'];
$item_qty = $row_total['item_qty'];
$item_price = $row_total['item_price'];
        $order_totals[$order_id] += $item_qty * $item_price;
$total_item_price += $item_qty * $item_price; // This is the line that adds all totals
}

 

Then $order_totals will be an array with the totals for each order, indexed by order_id.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.