Jump to content


Photo

Zip Code Lookup


  • Please log in to reply
7 replies to this topic

#1 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 13 September 2006 - 12:04 AM

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


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 September 2006 - 12:28 AM

Does your zipcode file include geo-coordinates, i.e lat/long for the centroid of each zip code?
Legend has it that reading the manual never killed anyone.
My site

#3 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 13 September 2006 - 12:38 AM

Yes it does.  about 42,000+ records



#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 September 2006 - 12:40 AM

http://www.google.ca...le Search&meta=

the first few are likely to contain useful code
Legend has it that reading the manual never killed anyone.
My site

#5 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 13 September 2006 - 10:28 PM

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


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 September 2006 - 10:48 PM

The calculation: http://www.zipcodewo...stance.php.html
Legend has it that reading the manual never killed anyone.
My site

#7 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 14 September 2006 - 12:13 AM

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
    <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>

Php 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;

?>


#8 ovihc

ovihc
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 17 July 2007 - 03:43 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users