Jump to content

Zip Code Lookup


usadarts

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
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

  • 10 months later...
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.