[Solved] Inserting months and updating it with Amount


Here’s an example script on how to do it as I explained in the comments before. All you have to do is adept your database and output to it:

<?php

$costs = 180; //Total costs
$month_costs = 30; //Costs per month
$paid = 80; //Paid first & second month + 10 dollar / euro extra
$months = $costs / $month_costs; //Total months

//Substract amount paid from total costs
$remaining_costs = $costs - $paid;

//Get the modulo value - This will be how much is remaining for
//the first month that isn't fully paid yet
$mod = $remaining_costs % $month_costs;

//Get the remaining months
$remaining_months = $remaining_costs / $month_costs;

//Floor the remaining months
//So if $remaining_months = 2.354 etc, it becomes 2
$floored_months = floor($remaining_months);

//Substract the floored value from $months
$open_months = $months - $floored_months;

//Echo out what is paid
for($i=1;$i<$months;$i++){
    if($open_months > $i){
        echo "Month ". $i .": paid.<br />";
    } else {
        break;
    }
}

//Echo out last month paid if fully paid
if($open_months >= $months){
    echo "Month ". $i .": paid.<br />";
}

//Echo out the amount remaining for next month if there's still something open:
if($remaining_costs > 0 && $mod != 0){
    echo "Month ". $i .": $". $mod ." remaining.<br />"; //$mod will return 10 because 2 x 30 and 1 x 20 was paid
} else {
    echo "Month ". $i .": paid.<br />";
}

//Echo out the remaining months if there are any:
if($i < $months){

    //Update $i
    $i++;

    //Echo out the remaining months:
    for($y=$i;$y<=$months;$y++){
        echo "Month ". $y .": $". $month_costs ." remaining.<br />";
    }
}

//Echo out if there's to much paid:
if($paid > $costs){
    $toMuch = $paid - $costs;
    echo "You have paid ". $toMuch ." to much.";
}

?>

Result:

Month 1: paid.
Month 2: paid.
Month 3: $10 remaining.
Month 4: $30 remaining.
Month 5: $30 remaining.
Month 6:
$30 remaining.

0

solved Inserting months and updating it with Amount