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>";
}
?>