imkesaurus Posted September 30, 2015 Share Posted September 30, 2015 Hi The user has 3 choices of either calculating A - transfer costs, B - bond costs or C - Ancillary costs which is A + B + C (C is just a fixed value added to the final costs of A + B) The user enters their purchase price which then shows all the costs for that option. I would like to know what would be the best way to calculate C? Here is my code so far (please ignore the fact that I don't have prepared statements yet, still trying to figure those out ): HTML: <h1>Transfer Cost Calculator</h1> <form action="calculate_cost.php" method="post"> Property Purchase Price: <input type="text" name="ppprice"> <input type="submit" value="Calculate"> </form> <h1>Bond Cost Calculator</h1> <form action="calculate_cost.php" method="post"> Property Purchase Price: <input type="text" name="pppriceb"> <input type="submit" value="Calculate"> </form> <h1>Ancillary Cost Calculator</h1> <form action="calculate_cost.php" method="post"> Property Purchase Price: <input type="text" name="pppricec"> <input type="submit" value="Calculate"> </form> PHP if(isset($_POST['ppprice'])){ $ppp = $_POST['ppprice']; echo $ppp . "<br>"; $sql = "SELECT * FROM transfer_cost ORDER BY ABS(purchase_price - $ppp) ASC, purchase_price ASC LIMIT 1 ;"; $result = $con->query($sql); while($row = $result->fetch_assoc()) { echo "Price: " . $row["purchase_price"]. " Duties: " . $row["transfer_duties"]. " Transfer: " . $row["transfer_fee"]. " Deeds: " . $row["deeds_office_fee"]. " VAT:" . $row["vat"]. " Total: " . $row["total_transfer_costs"]. "<br>"; $VAT = $row["vat"] + 238.6; $ttotal = $row["total_transfer_costs"] + 1697.4; $totalcost = $VAT + $ttotal; echo $VAT . "<br>" . $ttotal . "<br>" . $totalcost; } } elseif(isset($_POST['pppriceb'])){ $pppb = $_POST['pppriceb']; echo $pppb . "<br>"; $sql = "SELECT * FROM bond_cost ORDER BY ABS(purchase_price_b - $pppb) ASC, purchase_price_b ASC LIMIT 1 ;"; $result = $con->query($sql); while($row = $result->fetch_assoc()) { echo "Price: " . $row["purchase_price_b"]. " Reg Fee: " . $row["reg_fee"]. " Deeds: " . $row["deeds_office_fee_b"]. " VAT:" . $row["vat_b"]. " Total: " . $row["total_bond_cost"]. "<br>"; $VAT = $row["vat_b"] + 238.6; $ttotal = $row["total_bond_cost"] + 1697.4; $totalcost = $VAT + $ttotal; echo $VAT . "<br>" . $ttotal . "<br>" . $totalcost; } } I'm not sure if I should just run both statements again and calculate from there, but that seems like a lot of unnecessary code. If there is an easier way of doing it that would be wonderful. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/ Share on other sites More sharing options...
Jacques1 Posted September 30, 2015 Share Posted September 30, 2015 Create one function for each cost calculation. Then your calculate_ancillary_costs() can simply call calculate_transfer_costs() and calculate_bond_costs() and add up the values. By the way, you don't need a while loop when there's only one row like in this case. Just call fetch_assoc() once. Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/#findComment-1521966 Share on other sites More sharing options...
imkesaurus Posted October 1, 2015 Author Share Posted October 1, 2015 I don't quite understand how the functions will work though, I only get a total transfer or bond cost if the user enters it in one of the first two boxes, so if they input in the 3rd box then the first two statements won't be executed right? Or do I work the functions in differently? Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/#findComment-1522050 Share on other sites More sharing options...
hansford Posted October 1, 2015 Share Posted October 1, 2015 (C is just a fixed value added to the final costs of A + B) If that is the case, then it would be impossible to determine C without the known costs of both A and B. If you know the costs of A or B when the user selects either one of those values then you, as Jacques1 stated, call one function which calculates C based on the return values of calculate_transfer_costs() and calculate_bond_costs() Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/#findComment-1522054 Share on other sites More sharing options...
Solution Jacques1 Posted October 1, 2015 Solution Share Posted October 1, 2015 imkesaurus, it might make sense to forget about the user interface for now and just look at the logic: You want to be able to obtain one of three values: the transfer costs, the bond costs or the ancillary costs. All of those depend only on the purchase price, and the last one is derived from the other two. So why not create one function for each value? The function takes a purchase price (and a PDO object), selects the required data from the database, calculates the cost and then returns the result. For example: function calculate_transfer_costs($purchase_price, PDO $database_connection) { $transfer_costs_stmt = $database_connection->prepare(' SELECT transfer_duties, transfer_fee, deeds_office_fee, vat, total_transfer_costs FROM transfer_cost ORDER BY ABS(purchase_price - :purchase_price) ASC, purchase_price ASC LIMIT 1 '); $transfer_costs_stmt->execute(['purchase_price' => $purchase_price]); $transfer_costs = $transfer_costs_stmt->fetch(); // TODO: use constants instead of hard-coded numbers $vat = $transfer_costs['vat'] + 238.6; $total = $transfer_costs['total_transfer_costs'] + 1697.4; return [ 'transfer_duties' => $transfer_costs['transfer_duties'], 'transfer_fee' => $transfer_costs['transfer_fee'], 'deeds' => $transfer_costs['deeds_office_fee'], 'vat' => $transfer_costs['vat'], 'total_transfer_costs' => $transfer_costs['total_transfer_costs'], 'total' => $total, 'total_costs' => $vat + $total, ]; } Now you can call those functions and calculate the costs at any time in any order, no matter which button your user clicked. 2 Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/#findComment-1522110 Share on other sites More sharing options...
imkesaurus Posted October 2, 2015 Author Share Posted October 2, 2015 Thank you Jacques1 for the solution. You're really saving my job and teaching me a lot What a champion! Quote Link to comment https://forums.phpfreaks.com/topic/298376-calculate-result-based-on-separate-total-costs-displayed/#findComment-1522129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.