Click here to Skip to main content
15,034,131 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
So, i have this code selecting from payment and i want to calculate each payment base on the payment id in other to get the balance for each payment. Some payment id has five to seven payment and i want to be able get each payment multiply by intrest then the next payment multiply by intrest untill the last payment to get the balance on each payment id. I have no idea on how to use loop to achieve this. Kindly help`enter


What I have tried:

 $query = "SELECT * FROM payment WHERE application_id='".$sbank_lhistory['application_id']."' AND account='".$accno."' AND status='Verified!' ORDER BY id DESC";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($resul)){
$idk = $row['id'];
$name = $row['name'];
$paymentRequest = $row['paymentRequest'];
$paymentid = $row['payment_id']; 
}

 $counter = 0;
$arr = array("$paymentRequest");
while($counter < count($arr)){
echo $arr[$counter]*$Intrestps."<br>";
$counter++;
}
Posted
Updated 14-Sep-21 17:20pm
Comments
Member 15358161 15-Sep-21 7:58am
   
Thank you so much @Wendelius but i think i did not ask the questing in a right way. I can understand sum but my greatest challenge is, past payments is what i want to use to get user balance and the process is, e.g
Mr A first payment was $200
Mr A second payment was $300
Mr A third payment was $100
Mr A fourth payment was $500
Then, i want the loop to calculate the balance by this process:
1. $FixedAmount-$200*$intrest then get $NewFixedAmount
2. $NewFixedAmount-$300*$intrest then get $NewFixedAmount1
3. $NewFixedAmount1-$100*$intrest then get $NewFixedAmount2
4. $NewFixedAmount2-$500*$intrest then get $NewFixedAmount3

Then i can echo $NewFixedAmount3 as user balance.

1 solution

First of all, don't concatenate text directly to a SQL statement. This leaves you open to SQL injection - Wikipedia[^]. Instead, use parameters, see PHP MySQL Prepared Statements[^]

What comes to the actual question, I'd do the calculation in the query instead of fetching all the data and looping in the program. You didn't post the exact table structure but the calculation in the query could look something like
SQL
SELECT SUM(:interest * payment.amount) AS Total
FROM  payment 
WHERE payment.application_id = :application_id 
AND   payment.account = :accno
AND   payment.status = 'Verified!'

Note that bind variables have been used in both SELECT and WHERE clauses. For more examples about SUM, see MySQL SUM - Calculating The Sum of Values[^]

As a side note, using a piece of text to define the status is an error prone approach. Instead I would recommend using for example numeric, controlled, and enumerated values to indicate a status. For example 1 is open, 2 is cancelled, 3 is verified or which ever options you may have
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900