开发者

How to order php results from database by distance

开发者 https://www.devze.com 2023-04-12 05:25 出处:网络
I have a database of adverts which are searched and displayed using PHP. A user inputs their postcode during the search and then the query is executed. When the results page is returned to the user it

I have a database of adverts which are searched and displayed using PHP. A user inputs their postcode during the search and then the query is executed. When the results page is returned to the user it displays the distance the advert item is from their postcode.

What I开发者_如何学C need to know is how to order the result by distance. I can order the results for price by placing a sort on the query, but because the distance is calculated on the php page and is different for each user I don't know how to order it.

The distance is calculated using grids and Pythagoras theorem.

Not sure what code would be useful. If you want to see any just let me know.

Thanks


Don't reinvent the flat tire. You want the haversine geographic distance formula for this task. Look here.

MySQL Great Circle Distance (Haversine formula)

Once you have the distance worked, out, it's simple to order things correctly by distance and limit your search to make it efficient. Look here.

Geolocation distance SQL from a cities table

0

精彩评论

暂无评论...
验证码 换一张
取 消