Junebird Posted August 7, 2017 Share Posted August 7, 2017 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 More sharing options...
Sepodati Posted August 7, 2017 Share Posted August 7, 2017 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 More sharing options...
Barand Posted August 7, 2017 Share Posted August 7, 2017 Subtract the range minimum from the purchase price, Divide the result (integer division) by 50000, That will tell you how many 700's to add. Link to comment Share on other sites More sharing options...
Jacques1 Posted August 7, 2017 Share Posted August 7, 2017 intdiv() Link to comment Share on other sites More sharing options...
Barand Posted August 7, 2017 Share Posted August 7, 2017 ... or use floor() instead of intdiv() if you are not using PHPv7 yet. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.