Scooby08 Posted August 12, 2008 Share Posted August 12, 2008 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?? Quote Link to comment Share on other sites More sharing options...
btherl Posted August 12, 2008 Share Posted August 12, 2008 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. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 12, 2008 Author Share Posted August 12, 2008 Wow!! Matt really is the best!! Thanks a bunch.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.