UK Post Code Distance Calculator using PHP / MySQL - 3 super easy steps

Discussion in 'PHP' started by findmanu, Jun 1, 2010.

  1. #1
    Ok guys, just used this very nice little PHP script from ACMultimedia

    You need to give two post codes as input and it will return the distance between those two points. Please note that this is a UK specific script.

    All that you need to do it include about 3 files (including DB file) and just a couple of lines code. So simple. Wherever you need the distance, just include the following lines:

    Thats all. Once again, calculate distance in 3 simple steps

    (1) Import SQL - postcodes.sql.sql
    (2) Add three files in your project folder - distCalc.php, getDistance.php and config.php
    (3) Call the two line code wherever you want to show the distance

    Cheers
     

    Attached Files:

    findmanu, Jun 1, 2010 IP
  2. adamjoony

    adamjoony Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Firstly, you require a database of post codes with their corresponding longitude/latitude values. Thankfully, this has already been compiled by which can be downloaded in CSV or SQL format. We’ve provided the files here for your convenience.

    Download the UK post codes database in CSV/SQL:

    Create your database table using these files and your ready to begin. You will see that the database contains the following fields:

    • Post code (first part)
    • Postal town (Uk-Postcodes-Towns.csv file only)
    • X-coordinate (metres)
    • Y-coordinate (metres)
    • Latitude
    • Longitude
    Getting the Latitude and Longitude Values

    We’re assuming you have 2 full post codes to calculate the distance between. We only need the first part of the post code so we must trim them accordingly. The second part of the post code is always 3 characters (first part can either be 2, 3, or even 4 characters). So, we need to remove all spaces and then trim 3 characters off the end to give us the first part:

    <?php

    #Convert the post code to upper case and trim the variable
    $postcode = strtoupper(trim($postcode));

    #Remove any spaces
    $postcode = str_replace(" ","",$postcode);

    #Trim the last 3 characters off the end
    $postcode_first = substr($postcode,0,strlen($postcode)-3);

    ?> We have our 2 post codes now ready to compare. We need to query our database to obtain the latitude and longitude values for each post code. Use the following query, replacing with correct field/table names where appropriate:
    “SELECT * FROM tblPostCode WHERE fldPostCode = ‘$postcode_first’ LIMIT 1″;
    Check to ensure a post code was found and then assign the variables. You may wish to use something like this: (repeating the code for the second post code)

    <?php

    Does the post code exist?
    if ($myrow["fldPostCode"] != ""){

    #Assign variables
    $code1_lat = $myrow["fldLatitude"];
    $code1_long = $myrow["fldLongitude"];

    }else{

    #post code not found

    }

    ?>
     
    adamjoony, Aug 9, 2011 IP
  3. manmade1234

    manmade1234 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The thread is titled as 'Postcode distance calculator'. Do you know how simple is it to calculate distance online: Check for online 'Postcode distance calculator UK' in goole, it will help you to get exact solution.
    postcode.org.uk
    It is working in all over Europe and is expanding its services
     
    manmade1234, Dec 7, 2011 IP