I want to move to next row in a column using a variable. Like I have a column dr
. Now when I have done calculations to first value of dr
, I want to go to next value in the column, and perform some calculations there, and then to third and so on.
Algorithm
$payment
from the user.$dr
from a table balance
.$debited_amount
= $dr
- $payment
. There can be three cases.a)
$debited_amount
< 0 // when the user pays more amount as he needs to pay forgp_no
= 1b)
$debited_amount
> 0) // when the user pays less amount as he needs to pay forgp_no
= 1c)
$debited_amount
= 0 // when the user pays exactly the same amount as he needs to pay forgp_no
= 1
balance
by setting $dr
= 0 for first group and exit;balance
by setting $dr
= $debited_amount
for first group and exit;balance
by setting $dr
= 0
for first group and select dr
for next group and goto step3 ;Below is the PHP code:
<?php
If(isset($_REQUEST['submit'])!='')
{
$member_no = $_REQUEST['member_no'];
$payment = $_REQUEST['payment'];
$sql = "create view balance as select gp_no,sum(dr) as dr from instalment WHERE member_no = '$member_no' group by gp_no" ;
$sql = "select * from balance";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$dr = $row["dr"];
}
}
$debited_amount = $dr - $payment ;
$payment = $payment - $dr;
$i = 1 ;
do
{
if ($debited_amount < 0 ) // when the user pays more amount as he needs to pay for gp_no = 1
{
$sql = "update balance set dr = 0 WHERE gp_no = '$i' " ;// dr will be updated by dr = 0;
$dr++; // and move to next dr for subratraction i.e., dr of gp_no = 2
$i++;
$debited_amount = $dr - $payment ;
$payment = $payment - $debited_amount ;
}
elseif ($debited_amount > 0) // when the user pays less amount as he needs to pay for gp_no = 1
{
$sql = "update balance set dr = $debited_amount WHERE gp_no = '$i' " ;// dr will be updated by dr = $debited_amount ;
$dr = $debited_amount ;
$payment = $payment + $debited_amount ;
}
else // when the user pays exactly the same amount as he needs to pay for gp_no = 1
{
$sql = "update balance set dr = 0 WHERE gp_no = '$i' " ;// dr will be updated by dr = 0;
$dr = 0;
}
} while ($payment !=0 );