considerthis
Member
Although tbh I wouldn't write to the database at all. The problem with doing that is that if two people are accessing the list at the same time, user 1's prefilter runs, updates the table, user 2's prefilter runs, updates the table, user 1's display code runs and shows user 2's results.
That's why I suggested doing this all in the onLoadData hook, doing the distance query (again) there, inserting that distance directly into the in-memory data structure and sorting it. That way multiple users won't stomp on each other's data.
So you'd basically take the code you have for the distance query, run it before the foreach() loop in the code I gave you, and build an array of distance keyed by contact_id. Then in that code I gave you, use $yourDistances[$row->contact_id] as $distance.
-- hugh
My code so far is as below. However, when I print the $group I can see them in the correct order but the list does not populate according to the order and I'm confused with the highlighted part of your reply. How/Do I need to order the list in the default.php using the distance element we created in the memory?
Code:
$app = JFactory::getApplication();
$lat = $app->input->get('home_page_search___lat', '');
$lon = $app->input->get('home_page_search___lon', '');
if (!empty($lat) && !empty($lon)) {
foreach ($args[0]->data as $group) {
foreach ($group as $row) {
var_dump();
$hgeocode_lat = $row->civicrm_address___geo_code_1;
$hgeocode_lng = $row->civicrm_address___geo_code_2;
$distance = distances($lat,$lon,$hgeocode_lat,$hgeocode_lng);
// calculate the $distance here, and insert it into the empty 'distance' element ...
$row->distance = $distance;
$row->distance_raw = $distance;
}
usort($group, function($a, $b) {
return $a->distance > $b->distance;
});
echo "<pre>";
print_r($group);
echo "</pre>";
}
}
function distances($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return round($miles, 2);
}
Thanks in advance.
Bastiyan
Last edited: