I think you would be better to constitute a database yourself that would give distance between 2 locations.
Or, if you can get the latitude/longitude pair of those locations, the distance can be computed from the database itself.
Otherwise, if you can get a link of the 2 database, you could do an aggregation of the 2 results set, but most likely, you only have access to a front end of the distance database.
In this case, but it will be slow, what you need to do is
1) run your query on your local database
2) for each results (non paginated), do a query of the distance, and store the distance in an array like this:
PHP Code:
$ary[$distance][]=$rowId;
This will construct an array, with the distances as a key, and every database rows id as an 2nd dimension array
3) sort your 1st dimension
4) display the results
Of course, if your user has 10 or so results, it will work.
But if he has 300, this will be so slow that he will close the browser before every distance have been computed.
So really, all you got to do is fetch the latitude/longitude of each locations you have in your db, and then the computation is a simple
Code:
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
FROM yourTable
HAVING distance <= 10
ORDER BY distance DESC
Where $lon and $lat are the coordinate of the center of the radius (the origin)
http://zcentric.com/2010/03/11/calcu...and-longitude/
http://www.marketingtechblog.com/tec...late-distance/
Simply calculate this.
Create a table with every places, enter the latitude/longitude of each places (google hearth is handy to find them) and don't bother anymore with that second db.