It is currently Wed May 22, 2013 11:22 pm

All times are UTC


You can access below Applications



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: how to search nearest location using postcode in php ?
PostPosted: Tue Aug 31, 2010 8:05 am 
Offline

Joined: Tue Mar 02, 2010 11:25 am
Posts: 190
Location: Kovilpatti
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.php


Code:

<?php
$mysql_host 
= "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "postcodel";

mysql_connect($mysql_host,$mysql_username,$mysql_password)or die(mysql_error());//connected to mysql
//echo "Connected to SQL";
mysql_select_db($mysql_database) or die (mysql_error());//connect to database
//echo "connected to database "; 
?>
<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.php

Code:

<?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) {
    // Settings for if you have a different database structure
    $table="uk_postcodes";
    $lat="latitude";
    $lon="longitude";
    $postcode="postcode";
    $town="town";

    // This is a check to ensure we have a database connection
    if (!@mysql_query('SELECT 0')) { return; }

    // Simple regex to grab the first part of the postcode
    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]);

    // For debug only…
 //   echo "The distance between postcode: $from and postcode: $to is $distance miles\n"."<br>";

    return $distance;
    }
    
    function distance
($lat1, $lon1,$town1, $lat2, $lon2, $town2,$u='1') {

    $u=strtolower($u);
    if ($u == 'k') { $u=1.609344; } // kilometers
    elseif ($u == 'n') { $u=0.8684; } // nautical miles
    elseif ($u == 'm') { $u=1; } // statute miles (default)

    $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); // apply unit
    $d=round($d); // optional
    return $d;
    }
    
    function closest 
($needle,$haystack) {
    if (!$needle || !$haystack) { return; }
    if (!is_array($haystack)) { return; }

    $smallest=$_post['distance']; //smallest value
    $dist="";   
    foreach 
($haystack as $key => $val) {
    if ($val == $smallest) {  $dist.=$key.",";  }
    }
    return $dist;
    }

?>
Share |

_________________
Thanks,
A.ThangaSornam,

QualityPointTechnologies,
Ottapidaram.


Top
 Profile  
 
 Post subject: Re: how to search nearest location using postcode in php ?
PostPosted: Wed Oct 06, 2010 11:50 am 
Offline

Joined: Tue Mar 02, 2010 11:25 am
Posts: 190
Location: Kovilpatti
Hi

Here is the working demo of above sample code

clickhere

check how the postcode works
Share |

_________________
Thanks,
A.ThangaSornam,

QualityPointTechnologies,
Ottapidaram.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron