Jump to content

Archived

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

usadarts

Zip Code Lookup

Recommended Posts

Good Evening,

I need to write a program that will allow the end-user to enter their zip code and a max radius and come back with organizations within the radius they specified.

I already have the zipcode file and the organizations file.  I am unaware on how to begin this.  I assume it is some sort of algorithm.  Are their any sample codes out there?

Thanks,
David

Share this post


Link to post
Share on other sites
Does your zipcode file include geo-coordinates, i.e lat/long for the centroid of each zip code?

Share this post


Link to post
Share on other sites
http://www.google.ca/search?hl=en&q=php+calculate+distance+zip+codes&btnG=Google+Search&meta=

the first few are likely to contain useful code

Share this post


Link to post
Share on other sites
The links are more for distance between 2 zip codes.  I need the radius from a certain zip code.  Someone will enter in a Zip and Max distance and all the towns in the area will appear.

I have the complete Zip code file with lat/long coordinates.  I am assuming someone would enter in a zip.  I would search the zip file to find the lat and long coordinates.  I would then do some sort of algorithm to add/substract from those coordinates and then bring up the zip code within that new range.  Am I in the right direction?

David

Share this post


Link to post
Share on other sites
The calculation: http://www.zipcodeworld.com/samples/distance.php.html

Share this post


Link to post
Share on other sites
Thank you Andy.  Can someone let me know if I am headed in the right direction?  I have the following code below and the page comes up blank.  Any ideas?

Form Code[code]    <form action="ZipRadius.php" method="POST">
      <b><font color="#FFFFFF" size="5">Search the Database for <br>
      Local Affiliations</font></b> <br>
      <br>
      <table>
        <tr>
          <td><b><font color="#FFFFFF" size="3">Enter Zip Code:</font></b></td>
          <td>
            <input type="text" name="localzip" size="10">
          </td>
        </tr>
        <tr>
          <td><b><font color="#FFFFFF" size="3">Select Max Distance:</font></b></td>
          <td>
            <select name="radius">
              <OPTION VALUE="5">5 Miles
              <OPTION VALUE="10">10 Miles
              <OPTION VALUE="15">15 Miles
              <OPTION VALUE="20">20 Miles
              <OPTION VALUE="25">25 Miles
              <OPTION VALUE="30">30 Miles
              <OPTION VALUE="35">35 Miles
              <OPTION VALUE="40">40 Miles
              <OPTION VALUE="50">50 Miles
              <OPTION VALUE="75">75 Miles
              <OPTION VALUE="100">100 Miles
              <OPTION VALUE="125">125 Miles
              <OPTION VALUE="150">150 Miles
              <OPTION VALUE="175">175 Miles
              <OPTION VALUE="200">200 Miles
              <OPTION VALUE="225">225 Miles
              <OPTION VALUE="250">250 Miles
              <OPTION VALUE="275">275 Miles
              <OPTION VALUE="300">300 Miles
              <OPTION VALUE="325">325 Miles
              <OPTION VALUE="350">350 Miles
              <OPTION VALUE="375">375 Miles
              <OPTION VALUE="400">400 Miles
            </select>
          </td>
        </tr>
        <tr>
          <td colspan="2" align=center>
            <input type="submit" name="Submit" value="Lookup Affiliations">
          </td>
        </tr>
      </table>
     
    </form>
[/code]

Php Code [code]<?php
$localzip = $_POST['localzip'];
$radius = $_POST['radius'];

$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Database");

$queryZ = "SELECT * FROM zipData WHERE zipcode = $localzip";
$resultZ = mysql_query($queryZ,$dbh) or die(mysql_error());
$zipcode1 = $row['zipcode'];
$lat1 = $row['lat'];
$lon1 = $row['lon'];

$queryS = "SELECT * FROM zipData";
$resultS = mysql_query($queryS,$dbh) or die(mysql_error());

$results_header =<<<HEADER
<table cellspacing='1' align="center">
HEADER;

while($row = mysql_fetch_array($resultS))
{
$zipcode2 = $row['zipcode'];
$lat2 = $row['lat'];
$lon2 = $row['lon'];
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

if($miles <= $radius){
$queryA = "SELECT * FROM affiliations WHERE zip = $zipcode2";
$resultA = mysql_query($queryZ,$dbh) or die(mysql_error());

$results_details .=<<<DETAILS
<tr>
<td>$org</td>
<td>$address</td>
<td>$city</td>
<td>$state</td>
</tr>
DETAILS;
}

}
$results_footer ="</table>";

echo $results_header;
echo $results_details;
echo $results_footer;

?>
[/code]

Share this post


Link to post
Share on other sites
Hi usadac,

I'm working on a project that is very similar to what you were asking last year. Did you find the solution to your problem? I would really like to hear how you solved this. Maybe you could post it on the forums so that others can benefit as well.

Thanks!

Edward

Share this post


Link to post
Share on other sites

×

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.