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.

Edited by Sepodati
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.