0

I have been searching the web to find code that will allow a user to enter in a postcode and a specific distance and perform a search. e.g. (postcode) dd1 (distance from drop down can be within 10, 50, 100, 200 miles) 50 miles.

I already have code where you can enter 2 postcodes to work out the distance and thought of amending that but not succeeding. the code is UK postcodes.org

<?php
$dbName="";
$dbUsername="";
$dbPassword="";

$fromPC=$_POST['fromPC'];
$toPC=$_POST['toPC'];

function getDistance($lat1, $long1, $lat2, $long2, $unit)
{
  if($unit=="miles"){
      $earth = 3960; //miles
  }else{
     $earth = 6371; //kilometres
  }

    //From co-ordinates
    $lat1 = deg2rad($lat1);
    $long1= deg2rad($long1);

    //To co-ordinates
    $lat2 = deg2rad($lat2);
    $long2= deg2rad($long2);

    // The Haversine Formula
    $dlong=$long2-$long1;
    $dlat=$lat2-$lat1;

    $sinlat=sin($dlat/2);
    $sinlong=sin($dlong/2);

    $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

    $c=2*asin(min(1,sqrt($a)));

    $d=round($earth*$c);

    return $d;
}

if( (!empty($fromPC)) && (!empty($toPC)) )
{
    mysql_connect('localhost','username','password');
    @mysql_select_db('dbname') or die( "Unable to select database");

  // basic cleaning of input
    $firstPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$fromPC ));
    $secondPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$toPC ));

    // get first details
    $query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode`="'.$firstPC.'";';
    $result = mysql_query($query);
    $first = mysql_fetch_row($result);
    $checkFirst=mysql_num_rows($result);

  // get second details
    $query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode`="'.$secondPC.'";';
    $result = mysql_query($query);
    $second = mysql_fetch_row($result);
  $checkSecond=mysql_num_rows($result);

  // ensure there were results to calculate with
  if( ($checkFirst<1) || ($checkSecond<1) ){
      $outputResults="Unrecognised postcode entered.";
  }else{
        $distance = getDistance($first[0], $first[1], $second[0], $second[1], "miles");
        $outputResults = "The distance between postcode: $firstPC and postcode: $secondPC is ".$distance." miles.";
  }

  // always close your connections !!
    mysql_close();
}

?>
<h1>UKPostcodes.org - distance calculator</h1>
<form action="uk_postcodes.php" method="post">
  Please enter the first part of the postcodes only:<br/><br/>
  From postcode: <input maxlength="4" name="fromPC"/><br/>
  To postcode: <input maxlength="4" name="toPC"/><br/>
  <input type="submit" />
</form>

<?php echo $outputResults?> 

Examples of what I am after can be found on postcode-distance.com, AutoTrader, as well as a few other sites.

  • The entire `mysql_` family of functions has been deprecated for some time. You should be using MySQLi or PDO in any new code you're producing. – user229044 Mar 09 '14 at 17:37
  • use google maps for that ;) Make an API Call and store result for later in a database like (ID, ZIP_From, ZIP_To, Distance). **mysql_*** is marked as **deprecated** – Adrian Preuss Mar 09 '14 at 17:39
  • @meagar thanks I know I need to update my code from mysql to mysqli etc. I just started using php and the book I used was an old one and used mysql statements. – Fredd 777 Mar 10 '14 at 11:19
  • @adrian preuB I haven't used google maps api before so now I will need to see what that involves and how easy it is or isn't. – Fredd 777 Mar 10 '14 at 11:20

1 Answers1

0

Are you looking for this: http://database.appserver.aeglobalresearch.com/radius.php?lat=51.00000000&lng=5.86666700&dist=10

I build this, it searches streetnames in radius of 10km around gps location: 51.00000000&lng=5.86666700

If yes, i can give you the complete radius.php file and you can modify it. Took me also a lot of search to get it working :)