Click here to Skip to main content
14,429,185 members
Rate this:
Please Sign up or sign in to vote.
I am fairly new to php and mysql, the php code below calculates the distance between two given postcode and returns the distance as output.

What I am trying to achieve is to be able to get distance values for all postcodes within the database table that have the same value as $secondPC, although their latitude and longitude values maybe different.

Any help will be most appreciated thanks.

$dbName=" ";
$dbUsername=" ";
$dbPassword=" ";


function getDistance($lat1, $long1, $lat2, $long2, $unit)

$earth = 3960; //miles
$earth = 6371; //kilometres
//From co-ordinates
$lat1 = deg2rad($lat1);
$long1= deg2rad($long1);
//To co-ordinates
$lat2 = deg2rad($lat2);
$long2= deg2rad($long2);
// The Haversine Formula
return $d;


if( (!empty($fromPC)) && (!empty($toPC)) )
@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);
// get second details
$query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `part_type` LIKE "'.$secondPC.'";';
$result = mysql_query($query);
$second = mysql_fetch_row($result);


// ensure there were results to calculate with
if( ($checkFirst<1) || ($checkSecond<1) ){
$outputResults="Unrecognised postcode entered.";
$distance = getDistance($first[0], $first[1], $second[0], $second[1], "miles");
$outputResults = "Nearest Store is: ".$distance." miles from you";
$term = "$toPC";
$qry = ("SELECT $first[0],$first[1],$second[0],$second[1], id FROM uk_postcodes WHERE part_type LIKE '%$term%'");
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
return $return;
function create_table($dataArr) {
echo "";
for($j = 0; $j < count($dataArr); $j++) { 
echo "".$dataArr[$j]."";
echo "";
$all = mysql_fetch_all($res);
echo "";
for($i = 0; $i < count($all); $i++) {
echo $all[$i];
echo $all[$i];
echo "
$dist = getDistance($first[0], $first[1], $all[$i][0], $all[$i][1], "miles");
echo $dist;
Updated 29-Mar-13 11:50am
Monster Maker 29-Mar-13 16:09pm
Code without pre-tag is like easter without friday..!!

Editors..please take care of that.!
[no name] 29-Mar-13 16:19pm
The above code is just the php, there is a separate code for the full html....there is no need to re-paste the entire code again, it's just the pre-tag missing. thanks
phil.o 29-Mar-13 16:31pm
You want people to take time reading your code ? Then, please, take the time to format it correctly. You know there is a "Improve question" button, don't you?
Thanks :)
Monster Maker 29-Mar-13 16:43pm

Thanks :)
Zoltán Zörgő 29-Mar-13 16:31pm
Why have you reposted this: ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100