Jump to content

Sorting by Zip code


pocobueno1388

Recommended Posts

I have a table that stores the users zip code and I'm trying to display users by distance (nearest to furthest). I'm hoping there is a simple way to where I don't have to get a database with lat/long values...but when I think about it I doubt there is a formula that can measure the distance between two zip codes.

 

I would appreciate if someone could point me in the right direction. If I do need to get a database with lat/long values, it would be great if you could refer me to a good one.

 

Thanks!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/222868-sorting-by-zip-code/
Share on other sites

Ok, so I figured out that I would need a database full of zip codes along with their latitude/longitude. I now have that,  but I still need to figure out how to setup the query to sort users by distance.

 

Here is the relevant table information tables:

 

TABLE users
zip

TABLE zipcodes
zipcode
latitude
longitude

 

So if a user is logged in, I want it to display the users nearest to THEIR zipcode. How would I setup this query??

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/222868-sorting-by-zip-code/#findComment-1152419
Share on other sites

Depending on how dynamic the sorting and calculations will be, you might want to look into something like Sphinx to index the data and give you fast distance calculations/sorting, assuming you're able to install and run programs on your web server:

 

http://sphinxsearch.com/

 

You'll also need the lat/lon in radians.

Link to comment
https://forums.phpfreaks.com/topic/222868-sorting-by-zip-code/#findComment-1152423
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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