Jump to content

[SOLVED] Adding Totals


phpretard

Recommended Posts

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

<?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

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.