usadarts Posted September 13, 2006 Share Posted September 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 13, 2006 Share Posted September 13, 2006 Does your zipcode file include geo-coordinates, i.e lat/long for the centroid of each zip code? Quote Link to comment Share on other sites More sharing options...
usadarts Posted September 13, 2006 Author Share Posted September 13, 2006 Yes it does. about 42,000+ records Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 13, 2006 Share Posted September 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
usadarts Posted September 13, 2006 Author Share Posted September 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 13, 2006 Share Posted September 13, 2006 The calculation: http://www.zipcodeworld.com/samples/distance.php.html Quote Link to comment Share on other sites More sharing options...
usadarts Posted September 14, 2006 Author Share Posted September 14, 2006 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] Quote Link to comment Share on other sites More sharing options...
ovihc Posted July 17, 2007 Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.