So a solution I have found by combining a few sources is this (3959 is for miles use 6371 for km). I have included a PHP file and a HTML table below in case this helps anybody else with this problem.
To start with you can get the data you need from here:
https://www.freemaptools.com/download-uk-postcode-lat-lng.htm
I would recommend using that site to create a basic MySQL table and import the data but be warned this will take a little while! Also either before or after I would run a query to delete all spaces so AA11 1AA becomes AA111AA, this will make form validation a lot simpler.
This mini walk through uses both the data and table construct from that link.
First of all you will need a user input form that will look something like this:
<form action="" method="POST">
<table>
<tr>
<td>Distance:</td>
<td><input type="number" id="distance" name="distance" required></td>
</tr>
<tr>
<td>Miles:<br>Kilometres:</td>
<td><input type="radio" id="unit" name="unit" value="3959" checked><br><input type="radio" id="unit" name="unit" value="6371"></td>
</tr>
<tr>
<td>Postcode:</td>
<td><input type="text" id="postcode" name="postcode" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" id="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
This form submits to the same page so in your .php file where this sits you will need to include this code to convert the postcode to a long / lat value:
<?php include 'credentials.php';
$distance = $_POST['distance'];
$unit = $_POST['unit'];
$postcode = str_replace(' ','',$_POST['postcode']);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT latitude, longitude FROM postcodelatlng WHERE postcode LIKE '$postcode'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$latitude = $row['latitude'];
$longitude = $row['longitude'];
}
} else {
echo "Postcode not found";
}
$conn->close();
?>
Make sure your have a credentials.php file to provide the connection details for your database for this to work.
Then directly underneath this php script we are going to put a second one which will return all postcodes within a certain radius. You can then adapt this script to meet your needs. I will personally be passing this script against a different and much smaller table to check if any records are within a certain radius on the CRM but for now and for demonstration purposes just keeping it simple!
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT *,
($unit * acos( cos( radians($latitude) ) * cos( radians(latitude) ) * cos( radians(longitude) - radians($longitude)) + sin(radians($latitude)) * sin(radians(latitude)) ))
as distance
FROM postcodelatlng
HAVING distance < $distance";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "" . $row['postcode']. "<br>";
}
} else {
echo "<br>0 results";
}
$conn->close();
?>
Main thanks to this post which this is used from:
Fastest Way to Find Distance Between Two Lat/Long Points