php - Filter database query by distance from user -


i have database of events , activities user can search. user able filter, example activities , events within 50km of me (lol km #canada). distance each user changes.

i have script calculates distance based on latitude , longitude , displays it. cannot figure out how display activities , events within x km's.

not sure code should post, can post whatever needed.

//mysqli results query $query= "select id,name activity active =1".$typequery.$eventtypequery.$catquery.$datequery.$keywordsquery." order $orderby $order limit $start_from,$perpage";  //count query $count = $mysqli->query("select count(*) `activity` active=1".$typequery.$eventtypequery.$catquery.$datequery.$keywordsquery."");  $row = $count->fetch_row();  $total_records = $row[0]; $total_pages = ceil ($total_records /$perpage);  if (isset($page)&&($total_pages>1)) {$nextpage = $page+1;} else {$nextpage=0;}  if ($page>1) {$prevpage=$page-1;} else {$prevpage=0;}  $result=$mysqli->query($query);  function distance($lat1, $lon1, $lat2, $lon2, $unit) { $theta = $lon1 - $lon2; $unit="k"; $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;   $unit = strtoupper($unit);    if ($unit == "k") {     return ($miles * 1.609344);   } else if ($unit == "n") {       return ($miles * 0.8684);     } else {         return $miles;       } } 

this how query stuff distance, enter $long , $lat of user , things close user (edit having close want have it)

//so example have 1 user  $lat = <something>; $long = <something>;  select <some other info want select>,  ((acos(sin($lat * pi() / 180) * sin($lat * pi() / 180) + cos($lat * pi() / 180) * cos($lat * pi() / 180) * cos(($long - <table>.longitude) * pi() / 180)) * 180 / pi()) * 60 * 1.1515) distance  <your table> <maybe filters here?> having distance<=10000 order distance = '' desc, distance asc 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -