richrock Posted September 23, 2008 Share Posted September 23, 2008 I'm trying to figure this out. I've managed totals, percentages, and subtracting. But this I can't seem to figure: $getPayments = "SELECT * FROM jos_bid_payments WHERE salenum = ".$catnum." AND userid = ".$clientID.""; $resPayments = mysql_query($getPayments) or die(mysql_error()); while ($rowsPaid = mysql_fetch_array($resPayments)) { $payment = $rowsPaid['pay_method']; $amount = $rowsPaid['amount']; if ($amount > 0) { echo $payment.": £".number_format($amount,2)."<br />"; } } Some results will have multiple amounts, depending on payment methods. These are all returned by $amount. How can I get the total of all $amounts per person? Obviously these are displayed per $clientID, but I really have no clue how to total them up... ??? Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/ Share on other sites More sharing options...
JonnoTheDev Posted September 23, 2008 Share Posted September 23, 2008 while ($rowsPaid = mysql_fetch_array($resPayments)) { $payment = $rowsPaid['pay_method']; $amount = $amount + $rowsPaid['amount']; } print "Amount: ".$amount; Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/#findComment-648627 Share on other sites More sharing options...
richrock Posted September 23, 2008 Author Share Posted September 23, 2008 Thanks for that, but it doesn't quite work : In the DB I have two payments for an item, one of 2000.00 and another of 500.25 (to make sure decimals are carried through). The output I get now seems to create a sum for all results held...??? I'll look into seeing if I can split it further. Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/#findComment-648640 Share on other sites More sharing options...
JonnoTheDev Posted September 23, 2008 Share Posted September 23, 2008 Then why not use the MYSQL SUM() function to get each users total Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/#findComment-648654 Share on other sites More sharing options...
richrock Posted September 23, 2008 Author Share Posted September 23, 2008 SOLVED IT!!!! I needed to have the individual amounts as well as a running total of payments made. Clients may pay part on a card, the rest in a bank transfer. This is for items from a few hundred pounds to a few hundred thousand pounds... To see how I did it : $getPayments = "SELECT * FROM jos_bid_payments WHERE salenum = ".$catnum." AND userid = ".$clientID.""; $resPayments = mysql_query($getPayments) or die(mysql_error()); $total_payments = 0; while ($rowsPaid = mysql_fetch_array($resPayments)) { $payment = $rowsPaid['pay_method']; $amount = $rowsPaid['amount']; $total_payments += $rowsPaid['amount']; if ($amount > 0) { echo $payment.": £".number_format($amount,2)."<br />"; } } So now I can echo each result in the while loop and echo a total outside of the loop. And it works. I'm so chuffed. Thanks for your help and pointers. Rich Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/#findComment-648668 Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 or you could split the values in $rowsPaid['amount'] using split() at the , or " " space and then use those values and play with them Quote Link to comment https://forums.phpfreaks.com/topic/125466-solved-sum-of-one-variable-can-it-be-done/#findComment-648671 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.