dani33l_87 Posted July 8, 2014 Share Posted July 8, 2014 (edited) I have build a small form that take the zip code that user enter and if is on my list will redirect to a sub domain craigslist. I was checking all the zip codes and is pretty hard to know to what bigger city to attribute the given zip code and for that I was thinking to a simple idea to use, I am not sure how much exact will be but I think will do the trick for the moment. The zip code range start from 00501 to 99950. I create a array with the biggest city shown in craigslist and the zip code that correspond. Now the tricky part, what I will do with the rest of the zip codes and my idea is to measure the distance from one to another zip code and which zip code from the array is closer from given one to take it. <?php $zip = $_GET['zip']; $z = array ( 36830 => array ( 'auburn.craigslist.org' ), 35242 => array ( 'bham.craigslist.org' ), 36301 => array ( 'dothan.craigslist.org' ), 35630 => array ( 'shoals.craigslist.org' ), 35901 => array ( 'gadsden.craigslist.org' ) ?> Exemple: $zip= 36870 // the code received from the user 36870 - $array= ( 'auburn.craigslist.org' ), (36830 = 40) (36301 = 569) (36911 = -41) // the minus will be excluded (....etc) After checking the zip code with the ones from array, the smaller score will attribute the sub-domain correspond from the array. I hope will don t be the case when will get the same result from two zip codes:) Now I am using this rule: $zip0 = (isset($z[$zip][0]) && $z[$zip][0] != '') ? $z[$zip][0] : 'craigslist.org/about/sites#US'; and I was thinking to redirect the user for a page to enter the state and the city. But I wish to make it more precises and not to involved the user so much. But if there are any other suggestions I will be glad to hear them. Edited July 8, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/289582-finding-the-right-array-number-from-a-zip-code/ Share on other sites More sharing options...
ginerjm Posted July 8, 2014 Share Posted July 8, 2014 You do realize that even the SMALLEST cities have more than one zip code? IIRC cities use the first 3 digits to identify themselves - you might want to do some research on this. Quote Link to comment https://forums.phpfreaks.com/topic/289582-finding-the-right-array-number-from-a-zip-code/#findComment-1484300 Share on other sites More sharing options...
dani33l_87 Posted July 8, 2014 Author Share Posted July 8, 2014 yes, but this was my first thought and for the moment I think is the best. Other option is to use a form for the user to chose the state and the city, is also a option. Quote Link to comment https://forums.phpfreaks.com/topic/289582-finding-the-right-array-number-from-a-zip-code/#findComment-1484322 Share on other sites More sharing options...
Solution ginerjm Posted July 8, 2014 Solution Share Posted July 8, 2014 Much better choice IMHO Quote Link to comment https://forums.phpfreaks.com/topic/289582-finding-the-right-array-number-from-a-zip-code/#findComment-1484325 Share on other sites More sharing options...
dani33l_87 Posted July 9, 2014 Author Share Posted July 9, 2014 Ok, You are right, is the most secure and easy why to do it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/289582-finding-the-right-array-number-from-a-zip-code/#findComment-1484360 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.