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! Link to comment https://forums.phpfreaks.com/topic/104557-solved-adding-totals/ 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; Link to comment https://forums.phpfreaks.com/topic/104557-solved-adding-totals/#findComment-535209 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. Link to comment https://forums.phpfreaks.com/topic/104557-solved-adding-totals/#findComment-535210 Share on other sites More sharing options...
phpretard Posted May 7, 2008 Author Share Posted May 7, 2008 Simple as pie. Thank a million! Link to comment https://forums.phpfreaks.com/topic/104557-solved-adding-totals/#findComment-535215 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.