Jump to content

imkesaurus

New Members
  • Posts

    8
  • Joined

  • Last visited

imkesaurus's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thank you Jacques1 for the solution. You're really saving my job and teaching me a lot What a champion!
  2. 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?
  3. 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!
  4. HAHA! Wow I've been looking at code for too long. Completely missed that It's working perfectly. Thank you so so much for the help! I really appreciate it.
  5. Ok awesome thank you for helping! I really only know the basics off of what I've dealt with while building sites with Joomla or Wordpress Last stupid question though... How exactly do I insert that? I've done it like so: $sql = "SELECT * FROM transfer_cost ORDER BY ABS(purchase_price - <your input>) 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>"; } but then it gives me this error: Fatal error: Call to a member function fetch_assoc() on a non-object Do I need to call it differently? or am I doing the MySQL wrong?
  6. Hi Yes sorry that was terrible of me, I probably should have tried harder first before asking! The user inputs a number in the form. The form then goes and checks the first column for the closest value and then returns that row. I've managed to code up until where it only outputs the code if the number matches a value in the table. I'm now stuck on the closest value part. Table: calculator.php <!doctype html> <html> <head> <meta charset="utf-8"> <title>Calculator</title> </head> <body> <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> </body> </html> caclulate_cost.php: <?php $servername = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "xxx"; // Create connection $con = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if(isset($_POST['ppprice'])){ $ppp = $_POST['ppprice']; } echo $ppp . "<br>"; $sql = "SELECT purchase_price FROM transfer_cost"; $result=mysqli_query($con,$sql); $pparr = mysqli_fetch_all($result,MYSQLI_NUM); function getClosest($ppp, $pparr) { $closest = null; foreach($pparr as $item) { if($closest == null || abs($ppp - $closest) > abs($item - $ppp)) { $closest = $item; } } return $closest; } $sql = "SELECT * FROM transfer_cost WHERE purchase_price = $ppp"; $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>"; } ?>
  7. Hi there! I need some assistance. I'm a PHP noob and have been given a project to build a php 'calculator' for a client. Basically what needs to happen is the following - I have a table with predefined values, they are all numbers. The user will go to a form and add a number, this number then needs to be compared to the first column of the table after which it then needs to display only that row's values. As the numbers are predefined in the table the number the user enters then also needs to be rounded off to the nearest value found in the table. I've searched the internet and found snippets of code but can't quite understand how to put everything together. I understand how it should work, but just can't get there. I hope this all makes sense. If there is anyone who can help I would appreciate it a lot! Thanks
×
×
  • 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.