0

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 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

Max
  • 43
  • 2
  • 9
  • if it possible give more info about table structure and on the basis of what? you are calculating the distance, will be helpful for giving the result. – Punit Oct 10 '11 at 18:51

1 Answers1

1

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

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Thanks for your answer. I'll have a look at implementing this afternoon. Cheers – Max Oct 14 '11 at 10:53