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
https://forums.phpfreaks.com/topic/119256-solved-need-help-tweaking-code/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.