• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Custom code in Search Form

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:
Try ...

Code:
foreach ($args[0]->data as &$group) {
      foreach ($group as &$row) {

... with the & before those variables.

-- hugh
 
BTW, note that this technique won't be compatible with pagination. So you'll need to basically turn pagination off for the list, so all results show in one page.

-- hugh
 
BTW, note that this technique won't be compatible with pagination. So you'll need to basically turn pagination off for the list, so all results show in one page.

-- hugh
That might not work as I got more than 40k records in the DB and search results needs to be in pages.


Ill have a look around

Cheers
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top