phpretard Posted May 7, 2008 Share Posted May 7, 2008 I am trying to come up wth a grand total using multipple totals. Here is how I am getting the total: $result = mysql_query("SELECT * FROM invoices WHERE Cnumber='$Cnumber'"); while($row = mysql_fetch_array($result)) { $Cnumber=$row['Cnumber']; $qty=$row['qty']; $item=$row['item']; $description=$row['description']; $cost=$row['cost']; $date=$row['date']; $paid=$row['paid']; $total=(($qty)*($cost)).".00"; // THIS IS THE TOTAL $grandtotal=THE PROBLEM; } echo" <tr> <td id='qty'>$qty</td> <td id='item'>$item</td> <td id='description'>$description</td> <td id='cost'>$$cost</td> <td id='total'>$$total</td> </tr> "; } What the code above does is gives me a row in a table showing the individual totals. I have tried many different methods and can't seem to get one to add each row's total to come up with a grand total. I want to say that a foreach loop might solve the problem but I can't seem to code it right. Would love some help on this one. Thank errbody! Quote Link to comment Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 hi try this in your code $grandtotal += $total; Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 7, 2008 Share Posted May 7, 2008 <?php $grandtotal = 0; $result = mysql_query("SELECT * FROM invoices WHERE Cnumber='$Cnumber'"); while($row = mysql_fetch_array($result)) { $Cnumber=$row['Cnumber']; $qty=$row['qty']; $item=$row['item']; $description=$row['description']; $cost=$row['cost']; $date=$row['date']; $paid=$row['paid']; $total=(($qty)*($cost)).".00"; // THIS IS THE TOTAL $grandtotal += $total; } echo $grandtotal; ?> It should work this way. EDIT: Same idea as saint959 suggested, but i was writing the code in the meantime, so thought of submitting it anyway. Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 7, 2008 Author Share Posted May 7, 2008 Simple as pie. Thank a million! 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.