If you've created a model and schema for 'Store' you could load it up and then use something like this (completely untested):
$storeTable = $modx->getTableName('Store');
$query = new xPDOCriteria($modx,
'SELECT ROUND(
:earthRadius * ACOS(
SIN( :latitude*PI()/180 ) * SIN( latitude*PI()/180 )
+ COS( :latitude*PI()/180 ) * COS( latitude*PI()/180 ) * COS( (longitude*PI()/180) - (:longitude*PI()/180) ) )
, 1) AS distance,
store_name,
postcode,
postal_address,
latitude,
longitude
FROM
'.$storeTable.'
ORDER BY
distance ASC
LIMIT 0, 10',
array(
':earthRadius' => $earthRadius,
':latitude' => $latitude,
':longitude' => $longitude,
)
);
$stores = $modx->getCollection('Store',$query);
You might run into problems by repeating ':latitude'; if this is the case just use ':latitude1' and ':latitude2' with the same value.
If you don't have a model, you can at least use xpdo to get the results into an array using the same $query from above, and:
if ($query->stmt && $query->stmt->execute()) {
$stores = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
}
By the way, thanks for doing the math on that... looks useful!
[ed. note: okyanet last edited this post 14 years, 4 months ago.]