jaxdevil Posted November 2, 2007 Share Posted November 2, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 2, 2007 Share Posted November 2, 2007 <?php $total= count($row['amt']); echo $total; ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted November 2, 2007 Author Share Posted November 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 2, 2007 Share Posted November 2, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 2, 2007 Share Posted November 2, 2007 <?php $total= $row['amt']; if($total= $_GET['bidder']|| $total= $row['amt']) { $total= count($row['amt']); echo ("your total is: $total"); } ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted November 2, 2007 Author Share Posted November 2, 2007 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; ?> 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.