Jump to content

[SOLVED] calculate totals in a loop


jaxdevil

Recommended Posts

I am trying to calculate the totals of all the fields pulled up in a loop... i.e. I have a database called "checkout" that has bidder numbers, lot numbers, and amount for each lot number. When a person wins an auction (this is a live auction not an online auction) we enter a new row in the database that has the bidder number, the lot number, and the amount. After the auction is complete and they are checking out we have the script pull all the rows where the bidder number equals the bidder who is paying, it displays their bidder number, and lists each lot number and amount for that lot on separate lines in a loop statement. I would like to have a final line at the end of the page that displays the total of all the amounts listed. I.e. if they have lot number 12 at $50 and lot number 10 @ $27 and lot number 55 @ $63 the script displays as follows...

 

bidder number: 101

lot number 12: $50

lot number 10: $27

lot number 55: $63

 

And I would like the end to say

 

total: $140

 

How can I do this? I appreciate any help... Here is a copy of what I am working with now...

 

	 			<?php
$db_host = "localhost";
$db_user = "xxx_xxx";
$db_pwd = "xxxxxx";
$db_name = "xxx_xxx";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?> 
<?php
$sql = "SELECT * FROM checkout WHERE bidnum=$bidder";
$query = mysql_query($sql);

echo "Bidder Number:" .$_GET[bidder]. "<br>";

while($row = mysql_fetch_array($query)) {

echo "Lot Number" .$row['lotnum']. ":" .$row['amt']. "<br>";

}
?>

 

 

 

Link to comment
Share on other sites

Thanks for posting back so quick! I tried that just now, but it is just giving me the total number of entries for amt, not the total of the data (i.e. the total of all the prices in the row 'amt' for the fields that match to the bidder number)

 

There must be something else I can put in front, instead of count, is there a 'sum' function or something?

 

Thanks!

 

SK

Link to comment
Share on other sites

here you go

 

<?php
$sql = "SELECT * FROM checkout WHERE bidnum=$bidder";
$query = mysql_query($sql);

echo "Bidder Number:" .$_GET[bidder]. "<br>";

$intTotal = 0;
while($row = mysql_fetch_array($query)) {

echo "Lot Number" .$row['lotnum']. ":" .$row['amt']. "<br>";
$intTotal += $row['amt'];

}

echo "Your total is :".$intTotal;
?>

Link to comment
Share on other sites

Thanks guys! I had figured it out, I am sure those two methods above work perfect though. Here is what I used in case anyone wants it, that makes 3 ways to get this one to work so anyone else needing it can choose whatever works best for them. THANKS A MILLION GUYS!!

 

	 			<?php
$db_host = "localhost";
$db_user = "xxx_xxx";
$db_pwd = "xxxxxx";
$db_name = "xxx_xxx";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?> 
<?php
$sql = "SELECT * FROM checkout WHERE bidnum=$bidder";
$query = mysql_query($sql);

echo "Bidder Number:" .$_GET[bidder]. "<br>";
$sum = array();
while($row = mysql_fetch_array($query)) {
$sum[] = $row['amt'];
echo "Lot Number " .$row['lotnum']. ": $" .$row['amt']. "<br>";
}
$sum = array_sum($sum);
echo $sum;
?>

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.