cooldude832 Posted January 13, 2008 Share Posted January 13, 2008 I have a table full of only USA addresses and I want to have a thing that says within X miles of Zipcode 12345 Lets say its 10 miles how do I derive the Where Zip >= "12345-X" and ZIP <= "12345+x" Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/ Share on other sites More sharing options...
Ken2k7 Posted January 13, 2008 Share Posted January 13, 2008 I have a table full of only USA addresses and I want to have a thing that says within X miles of Zipcode 12345 Lets say its 10 miles how do I derive the Where Zip >= "12345-X" and ZIP <= "12345+x" Wait, that's not how distances work, or is it? ??? Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437618 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 Long/Lat would be the best way, Zip would be unreliable Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437619 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 Zipcode is very reliable and the zipcodes are assigned via location ebay does it, I just don't know how the United States distributes zipcodes so I thought I asked someone. and who in the world seriously knows their longitude/latitude position??? Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437622 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 I'm assuming I'm going to have to find a table of US Zipcodes that has their lognitude/lattitude position and then from that zip code convert the miles into a range, find all zipcodes in that range and then use it in my query. Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437623 Share on other sites More sharing options...
Hypnos Posted January 13, 2008 Share Posted January 13, 2008 There are free web APIs out there to convert addresses to Lat/Long. It's called geocoding. I don't know that any of them will just take a zip code. But there might be some out there. Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437624 Share on other sites More sharing options...
Ken2k7 Posted January 13, 2008 Share Posted January 13, 2008 How would zip codes tell distances anyways? There are only a handful of zip codes that exist anyways. Not every number can be a zip code. Say my zip code is 32154 and I want 1 mile away from both sides. So you're saying 32155 and 32153 are zip codes too? Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437626 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 How would zip codes tell distances anyways? There are only a handful of zip codes that exist anyways. Not every number can be a zip code. There isn't a "handful" of zip codes, its a number defining a region in the Untied states by the Us Postal service. Everyone knows the zipcode of their location. I think this is beyond your scope, I found a db dump on it, but I think there is a simplier way Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437627 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 I do and who in the world seriously knows their longitude/latitude position??? Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437636 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 well your unusualy what i came up with works <?php if(!empty($_GET)){ connectSQL(); $miles = intval($_GET['miles']); $zip = intval($_GET['zip']); if(strlen($zip) != 5){ echo "Invalid Zip"; } if(!empty($miles) && strlen($zip) == 5){ connectSQL(); $distance = $miles*(1/69); $q = "Select lng, lat from `zips` Where zip = '".$zip."'"; $r = mysql_query($q) or die(Mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ $row = mysql_fetch_assoc($r); $min['lat'] = round($row['lat']-$distance,5); $min['lng'] = round($row['lng']-$distance,5); $max['lat'] = round($row['lat']+$distance,5); $max['lng'] = round($row['lng']+$distance,5); $where = "lat >= '".$min['lat']."' and lat <= '".$max['lat']."' and lng >='".$min['lng']."' and lng <='".$max['lng']."'"; $q = "Select zip from `zips` Where ".$where; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ while($row = mysql_fetch_assoc($r)){ $zips[] = $row['zip']; } } else{ echo "No Records found.<br />".$q; } } else{ echo "Invalid Zip."; } } else{ echo "Not valid search parameters."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437671 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 Hey look at that, Lat and Long... Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437673 Share on other sites More sharing options...
cooldude832 Posted January 13, 2008 Author Share Posted January 13, 2008 Hey look at that, Lat and Long... yeah but thats like asking a person what their weight is in Newtons, they don't know, but they probably know their weight is in pounds so you convert it. I thought there was a very generic zip -> location (lat,long) system based on the numbers, but I guess not. Quote Link to comment https://forums.phpfreaks.com/topic/85746-within-x-miles-of-zipcode-logic/#findComment-437680 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.