Hi
how to find the nearest location using postcode ? i take the data from this site
click this for download . then load it in you db table name uk_postcodes
This file contain two textbox one for post code and another one for closest distance
after submit the form it list out the closest postcode according to what we give in the distance textbox. if we dont want to give distance in textbox means simply we assign the
Quote:
$smallest=min($haystack);
in the postcodefunction.php.
postcodesearch.phpCode:
<?php
$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "postcodel";
mysql_connect($mysql_host,$mysql_username,$mysql_password)or die(mysql_error());mysql_select_db($mysql_database) or die (mysql_error());?>
<html>
<body>
<form name="postcode" action="postcodefunction.php" method="post">
Postcode:<input type="text" name="postcode">
closestDistance:<input type="text" name="distance">
<input type="submit" name="postcodsearch" value="search">
</form>
</body></html>
the following file calculate the distance of the current postcode with the postcode in table.then compare the distance with the text box value what we submitted in form and return the postcode .
then
postcodefunction.phpCode:
<?php
include "config.php";
if (isset($_POST['postcode'])) {
$select="select postcode from uk_postcodes";
$query=mysql_query($select);
$postcodes=array();
while($row=mysql_fetch_assoc($query))
{
$postcodes[]=$row['postcode'];
}
$input=strtoupper($_POST['postcode']);
$closest=postcode_closest($input,$postcodes);
}
if (isset($closest)) {
echo "The closest postcode is:". $closest;
}
function postcode_closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }
foreach ($haystack as $postcode) {
$results[$postcode]=postcode_distance($needle,$postcode);
$results[$postcode]."for ".$postcode."<br>";
}
return closest($needle,$results);
}
function postcode_distance ($from,$to) {
$table="uk_postcodes";
$lat="latitude";
$lon="longitude";
$postcode="postcode";
$town="town";
if (!@mysql_query('SELECT 0')) { return; }
preg_match('/[A-Z]{1,2}[0-9R][0-9A-Z]?/',strtoupper($from),$match);
$one=$match[0];
preg_match('/[A-Z]{1,2}[0-9R][0-9A-Z]?/',strtoupper($to),$match);
$two=$match[0];
$sql = "SELECT $lat, $lon,$town FROM $table WHERE $postcode='$one'";
$query = mysql_query($sql);
$one = mysql_fetch_row($query);
$sql = "SELECT $lat, $lon,$town FROM $table WHERE $postcode='$two'";
$query = mysql_query($sql);
$two = mysql_fetch_row($query);
$distance = distance($one[0], $one[1],$one[2], $two[0], $two[1],$two[2]);
return $distance;
}
function distance($lat1, $lon1,$town1, $lat2, $lon2, $town2,$u='1') {
$u=strtolower($u);
if ($u == 'k') { $u=1.609344; } elseif ($u == 'n') { $u=0.8684; } elseif ($u == 'm') { $u=1; } $d=sin(deg2rad($lat1))*sin(deg2rad($lat2))+cos(deg2rad($lat1))*cos(deg2rad($lat2))*cos(deg2rad($lon1-$lon2));
$d=rad2deg(acos($d));
$d=$d*60*1.1515;
$d=($d*$u); $d=round($d); return $d;
}
function closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }
$smallest=$_post['distance']; $dist="";
foreach ($haystack as $key => $val) {
if ($val == $smallest) { $dist.=$key.","; }
}
return $dist;
}
?>