Kavvson Empcraft
Kavvson Empcraft

Reputation: 443

PHP Loop calculations, get previous value and add current

I am trying to get previous value for current loop for some calculations. As a starting point lets describe what does the code.

The idea is to make a forecast/prediction for the months having the base cost and default percentage for the months.

This is the logic for the loop. January is the starting month, we set there the base $price but next month( February ) should be January * $default_percent, March should be February * $default_percent etc. all should include corrections.

When it comes to the corrections we got two variables

$percentage = (!empty($modified_percentage)) ? $modified_percentage : $default_percent;
$month_cost = $price * $percentage + $correction_cost;

Percentage can change if outer loop month($m) equals to correction array $month, else its the default value. Month cost is a basic calculation.

  +----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+
  |   jan    |      feb      |    mar     |    apr     |    may     |    june    |    july     |    augu     |    sept     |     oct     |    nov     |    dece    |
  +----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+
  | 41290.65 | 41290.65*1.03 | feb * 1.03 | mar * 1.03 | apr * 1.03 | may * 1.03 | june * 1.03 | july * 1.03 | augu * 1.03 | sept * 1.03 | oct * 1.03 | nov * 1.03 |
  +----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+

Code snippet


<?PHP
$forecast_correction = array(
    array(
        'month' => '2',
        'add' => 150.00,
        'substract' => 0.00,
        'final' => 150.00,
        'percent' => 1.02
    ),  
    array(
        'month' => '3',
        'add' => 0.00,
        'substract' => 250.00,
        'final' => -250.00,
        'percent' => NULL
    ),  
    array(
        'month' => '4',
        'add' => 0.00,
        'substract' => 0.00,
        'final' => 0.00,
        'percent' => 0.15
    )   
);  
$price               = 41290.65;
$default_percent     = 1.03;

for ($m = 1; $m <= 12; ++$m) {
    $correction_cost     = 0;
    $modified_percentage = null;
    foreach ($forecast_correction as $correction) {

        if ($m == $correction['month']) {
            $correction_cost = $correction['final'];
            if (!empty($correction['percent'])) {
                $modified_percentage = $correction['percent'];
            }   
        }   
    }   
    ;   
    $percentage = (!empty($modified_percentage)) ? $modified_percentage : $default_percent;
    $month_cost = $price * $percentage + $correction_cost;

    echo date('F', mktime(0, 0, 0, $m, 1)) . " :: $month_cost :: " . $correction_cost . "  :: " . $percentage . "% \n";
}   

Question and desired output


Question : How to implement the February = January * percentage logic in this particular example to have all the results properly calculated with the correction array.

Desired output ( can be plain text, array whatever. )

Month : Cost

If you need future clarification don't hesitate to ask, I am quite stuck with this one.

MCVE sandbox


http://sandbox.onlinephpfunctions.com/code/0c705ab721c6160813b98d133d8a1b7ff876cdca

Upvotes: 2

Views: 660

Answers (1)

Clydog
Clydog

Reputation: 108

I think with a couple of minor modifications your example code will do what you want.

I renamed $price to $base_month_cost, where it is initially defined:

$base_month_cost     = 41290.65;

and where it is used to calculate $month_cost in the outer loop:

$month_cost = $base_month_cost * $percentage + $correction_cost;

Then, after the line (above) that calculates $month_cost, I added a new line to redefine $base_month_cost:

$base_month_cost = $month_cost;// set here for use in next iteration

This means that the $month_cost for each successive month is based on the previous month's $month_cost.

You also want to ensure that the no percentage increase is applied when calculating the $month_cost for January. I can think of two approaches to this problem:

  1. You could add January to the $forecast_correction array with a 'percent' value of 1 (so the initial $base_month_cost gets multiplied by 1, and is unchanged).
  2. You could add some additional logic within the for loop, either to set $percentage to 1 if $m == 1, or to do a different calculation of $month_cost, ignoring $percentage if $m == 1.

I think the first approach is cleaner.

Hopefully this is what you're looking for. When I ran it for a test with my modifications, I noticed that the percent value for April is 0.15 in your example, which significantly reduces the $month_cost for that (and successive months).

Upvotes: 2

Related Questions