phpanon Posted March 11, 2008 Share Posted March 11, 2008 Hello there, I am producing a simple system that monitors internal orders. At the moment when displaying all completed orders the total does not seem to be working properly. the first order, the total is worked out fine. but the second order takes the first order's total and adds it on to its own and displays the total of that. This happens for each order after that until the last order where the total is enormous. here is my code <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location: login.php"); exit(); } require "connect.php"; $query = "SELECT DISTINCT o.orderID FROM orders o, productorder p WHERE p.orderID = o.orderID AND p.status = 'Complete' AND o.empID = ".$_SESSION['empID']; $result = mysql_query ($query, $connection) or die ("Unable to perform query<br>$query"); ?> <!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Past Orders</title> <link rel="stylesheet" type="text/css" href="mystylelogin.css" /> </head> <body> <?php switch ($_SESSION['type']) { case 'user': include ("header.php"); include ("StationaryMenu.php"); break; case 'admin': include ("headerAdminMain.php"); include ("StationaryMenu.php"); break; case 'manager': include ("header.php"); include ("StationaryMenuManager.php"); break; } ?> <div id="header">Past Orders</div> <div id="OrdersBasketTable"> <table border="0" width="100%"> <hr /> <tr> <th width="100">Order Number</th> </tr> <?php while($row=mysql_fetch_array($result)) {?> <tr> <td align="center"><h2><strong><?php echo $row['orderID']?></strong></h2></td> <td> <table border="0"width="100%"> <tr> <th width="25%" align="left">Product</th> <th width="10%" align="left">Price</th> <th width="10%" align="left">Quantity</th> <th width="18%" align="left">Date Made</th> <th width="14%" align="left">Date Approved</th> <th width="13%" align="left">Total</th> </tr> <tr> <?php $query2 = "SELECT * FROM orders o, productorder e, product p WHERE o.orderID = e.orderID AND p.URN = e.URN AND e.status = 'Complete' AND o.orderID = '".$row['orderID']."'"; $result2 = mysql_query ($query2, $connection) or die ("Unable to perform query<br>$query2"); while($row2=mysql_fetch_array($result2)) { ?> <tr> <td><a href="productOrder.php?URN=<?php echo $row2['URN']?>"><?php echo $row2['productName']?></td> <td>£<?php echo $row2['price']?></td> <td align="center"><?php echo $row2['quantity']?></td> <td><?php echo $row2['date']?></td> <td><?php echo $row2['apprDate']?></td> <?php $total = $row2['quantity'] * $row2['price']; ?> <td>£<?php echo $total ?></td> <?php $total2 = $total2 + $total ?> </tr> <?php } ?> </td> </tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td><strong>Total</strong></td> <td>£<?php echo $total2 ?></td> <td></td> </table> <?php } ?> </table> </td> </table> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/95676-totals-keep-adding-on/ Share on other sites More sharing options...
deadonarrival Posted March 11, 2008 Share Posted March 11, 2008 You don't ever set $total2 back to zero, so every time you run the line <?php $total2 = $total2 + $total ?> It doesn't distinguish between orders, and adds it on. Replace this line <td>£<?php echo $total2 ?></td> With <td>£<?php echo $total2; $total2 = 0; ?></td> Or alternatively replace while($row=mysql_fetch_array($result)) {?> with while($row=mysql_fetch_array($result)) { $total2 = 0; ?> Actually, the second is more conventional - go with that Link to comment https://forums.phpfreaks.com/topic/95676-totals-keep-adding-on/#findComment-489832 Share on other sites More sharing options...
phpanon Posted March 11, 2008 Author Share Posted March 11, 2008 thank you.. that did it! Link to comment https://forums.phpfreaks.com/topic/95676-totals-keep-adding-on/#findComment-489843 Share on other sites More sharing options...
deadonarrival Posted March 11, 2008 Share Posted March 11, 2008 No problem, glad it helped Next time you need to debug a loop, try going through it manually. Time consuming at times, but writing down the line number and what the value is can help a lot sometimes. Link to comment https://forums.phpfreaks.com/topic/95676-totals-keep-adding-on/#findComment-489912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.