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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.