Jump to content

Increments and loops for calculator?


Junebird

Recommended Posts

Good day,

I am new to PHP and I am trying to figure out a way to create a calculator that works out additional fees to a purchase price.

The client would have to fill in a purchase price, then using if statements the purchase price would then be compared to a price range AND an additional basefee of 4600 for that price range is added to the purchaseprice later on

 

if ($purchaseprice > 100 000 && $purchaseprice <= 500 000) {$basefee = 4600;}

 

Then I need it to determine with in that range, for EVERY 50 000 ontop of the smallest $purchaseprice amount (100 000 in this case) it must add 700 to the 4600 $basefee

for example

if ($purchaseprice >150 000 &&  $purchaseprice <=200 000 ) {$basefee + 1400;}

 

To write if statements for every 50 000 up to 10 000 000 would take forever and isn't efficient.

From my rudimentary understanding one would have to use increments and loops?

I am completely unsure as to how to move forward?

Any assistance would be greatly appreciated.

Link to comment
Share on other sites

Use math to determine how many $50,000 increments are in the price minus $100k. Something like this, for example:

 

<?php
$base = 100000;
$purchaseprice = 150000;
$basefee = 4600;
$addfee = 700;
$addincrement = 50000;

$tempprice = max($purchaseprice - $base, 0);
$increments = ( $tempprice > 0 ? intdiv($tempprice, $addincrement) : 0);
if($purchaseprice % $addincrement == 0) { $increments++; }

$finalprice = $purchaseprice + $basefee + ($increments * $addfee);

print "Purchase price: {$purchaseprice}, Base Fee: {$basefee}, Increments: {$increments} x {$addfee}, Final Price: {$finalprice}";

?>
Try it here: https://3v4l.org/ilRVo

 

Note that this determines $100,000 - $149,999 as one $700 addition, $150,000 - $199,999 as two additions, etc. Adjust accordingly.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.