Jump to content

Postcode/zipcode distance calculator?


amg182

Recommended Posts

Hi guys,

 

Been looking around for script that could lookup a zipcode/postcode field stored within mysql database and return a distance value(miles) based on what postcode a user has inputed.

 

Anyone come across these before, or know how i could implement this into my database results?

 

I have found and stored some scripts that can calcutate distance by entering two postcodes/zipcodes-but none that can take the postcode field within a database into the equation.... could a script like this be altered to take this into account?

 

Any feedback would be greatly recieved.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/240382-postcodezipcode-distance-calculator/
Share on other sites

Yeah, I have the follwing script that works fine. It echoes the distance from one destination to another. In the script below, you can see it calculates the distance from Pcode A to Pcode B. (Pcode A = ME14 and Pcode B = TN24)-These are just random towns in the UK. The only thing is you have to enter the postcodes directly into the script as static text.

 

So what I would like to happen is the following:

 

(1) User enters their postcode into a input textbox (Which will be Pcode A).

(2) The results will then show the distance between the users postcode entered(Pcode A) and the "clients" postcode stored within the database -which are stored in mysql field called postcode(which will be Pcode B).

 

<?php

$dbcnx = mysql_connect("localhost",

"root", "");

if (!$dbcnx) {

echo( "

Unable to connect to the " .

"database server at this time (this is a host connect problem).
" );

exit();

}

// Select the database

if (! mysql_select_db("postcodes", $dbcnx) ) {

echo( "

Unable to locate the " .

"database at this time(this is a dbconnect problem).
" );

exit();

}

function calc_postcode_seperation($pcodeA,$pcodeB)

{

// PCODE A

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$pcodeA' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[0]=$row[Grid_N];

$gride[0]=$row[Grid_E];

// PCODE B
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$pcodeB' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[1]=$row[Grid_N];

$gride[1]=$row[Grid_E];

// TAKE GRID REFS FROM EACH OTHER.

$distance_n=$gridn[0]-$gridn[1];

$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS

$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e));

$text.='Distance between '.$pcodeA.' and '.$pcodeB.' is: '.round($hypot/1000,2).'kms';
return $text;
}
$from=$from;

echo calc_postcode_seperation('ME14', 'TN24');

?>

 

As you can see I have a database and table that holds the postcode information(Grids, latitude and longitude).  But my "clients" table is within a seperate database.

 

Thanks

I have tiedied up the previous code. All that needs to be done is to assign the 2 variable($from and $to).

 

Can someone please help me with this? This is about as far as I can get with my very limited experience.

 

I would like to have the $from variable value based on the value entered within a input textbox. And the $to variable to be a field within my talble called postcode.

 

Is anyone able to help me?

 

<?php

$dbcnx = mysql_connect("localhost",

"root", "");

if (!$dbcnx) {

echo( "

Unable to connect to the " .

"database server at this time (this is a host connect problem).
" );

exit();

}

// Select the database

if (! mysql_select_db("postcodes", $dbcnx) ) {

echo( "

Unable to locate the " .

"database at this time(this is a dbconnect problem).
" );

exit();

}

function calc_postcode_seperation($pcodeA,$pcodeB)

{ 

// Set the variables from=Source  to=Destination
$from='ME14';
$to='TN24';

// From Variable Pcode

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$from' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[0]=$row[Grid_N];

$gride[0]=$row[Grid_E];

// TO variable
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$to' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[1]=$row[Grid_N];

$gride[1]=$row[Grid_E];

// TAKE GRID REFS FROM EACH OTHER.

$distance_n=$gridn[0]-$gridn[1];

$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS

$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;

$text.='Distance between '.$from.' and '.$to.' is: '.round($hypot/1000,2).'Miles';

return $text;

}

echo calc_postcode_seperation('', '');


?>

 

 

Thanks again!

Got it working!  The only thing is it has some error messages that i cant seem to get rid of. Although it is working fine.

 

Notice: Use of undefined constant Grid_N - assumed 'Grid_N' in C:\xampp\htdocs\testing\testing\post.php on line 75

 

Notice: Use of undefined constant Grid_E - assumed 'Grid_E' in C:\xampp\htdocs\testing\testing\post.php on line 77

 

Notice: Use of undefined constant Grid_N - assumed 'Grid_N' in C:\xampp\htdocs\testing\testing\post.php on line 85

 

Notice: Use of undefined constant Grid_E - assumed 'Grid_E' in C:\xampp\htdocs\testing\testing\post.php on line 87

 

<form action="post.php" method="get">
Postcode: <input type="text" name="postcode" />
<input type="submit" />
</form> 


Your Postcode: <?php echo $_GET["postcode"]; ?>.
<?php

$pcode=$_GET["postcode"];

?>

<?php

$dbcnx = mysql_connect("localhost",

"root", "");

if (!$dbcnx) {

echo( "

Unable to connect to the " .

"database server at this time (this is a host connect problem).
" );

exit();

}

// Select the database

if (! mysql_select_db("postcodes", $dbcnx) ) {

echo( "

Unable to locate the " .

"database at this time(this is a dbconnect problem).
" );

exit();


}


//finds value from database field

$from = mysql_query("SELECT * FROM `location` WHERE id='1'");



while($row = mysql_fetch_array($from))
  {
  


$start=$pcode;

$fin=$row['to'];
}

{
// PCODE A

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[0]=$row[Grid_N];

$gride[0]=$row[Grid_E];

// PCODE B

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[1]=$row[Grid_N];

$gride[1]=$row[Grid_E];

// TAKE GRID REFS FROM EACH OTHER.

$distance_n=$gridn[0]-$gridn[1];

$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS

$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e))/1.609;


}

echo 'Distance between '.$start.' and '.$fin.' is '.round($hypot/1000,2).' Miles';

?>

 

Any idea why its got these errors?

I believe its because your missing '' around the $row valuse Grid_N and Grid_E

 

change

<?php
// PCODE A

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[0]=$row[Grid_N];

$gride[0]=$row[Grid_E];

// PCODE B

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[1]=$row[Grid_N];

$gride[1]=$row[Grid_E];
?>

 

to

<?php
// PCODE A

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$start' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[0]=$row['Grid_N'];

$gride[0]=$row['Grid_E'];

// PCODE B

$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$fin' LIMIT 1");

$row=mysql_fetch_array($result);

$gridn[1]=$row['Grid_N'];

$gride[1]=$row['Grid_E'];
?>

 

remove the <?php ?> tags I added, they are there to show you the difference

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.