Skip to content

Instantly share code, notes, and snippets.

@mssyogi
Last active February 16, 2018 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mssyogi/db8dd1202b3f80973900badb248b3c94 to your computer and use it in GitHub Desktop.
Save mssyogi/db8dd1202b3f80973900badb248b3c94 to your computer and use it in GitHub Desktop.
nearbydistance
SELECT id, 111.045 * DEGREES(ACOS(COS(RADIANS(@lat))
* COS(RADIANS(lat))
* COS(RADIANS(lng) - RADIANS(@lng))
+ SIN(RADIANS(@lat))
* SIN(RADIANS(lat))))
AS distance_in_km
FROM airports
ORDER BY distance_in_km ASC
LIMIT 0,5;
public function getNearByUsers()
{
$data = $this->getRequestParams();
$userId = $data['user_id'];
$lat = $data['latitude'];
$lng = $data['longitude'];
$km = !empty($data['distance']) ? $data['distance'] : 100;
$page = !empty($data['page']) ? $data['page'] : 1;
$limit = !empty($data['limit']) ? $data['limit'] : 50;
$start = $page*$limit - $limit;
$end = $page*$limit;
$req_query = "SELECT id,user_id,latitude,longitude, 111.045 * DEGREES(ACOS(COS(RADIANS($lat))
* COS(RADIANS(latitude))
* COS(RADIANS(longitude) - RADIANS($lng))
+ SIN(RADIANS($lat))
* SIN(RADIANS(latitude))))
AS distance_in_km
FROM parked_location HAVING distance_in_km < $km
ORDER BY distance_in_km ASC
LIMIT $start,$end";
$result = mysql_query($req_query);
if ($result) {
$location = array();
$count=0;
while ($row = mysql_fetch_assoc($result)) {
//Do stuff
array_push($location, $row);
$count++;
}
$response = array('Status' => 'success', 'Message' => 'records found','count'=> $count, 'Result' => $location);
} else {
$response = array('Status' => 'failed', 'Message' => 'Error try later', 'Result' => array());
}
$this->output($response);
}
public function savelocation()
{
$data = $this->getRequestParams();
$userId = $data['user_id'];
try {
$validate = mysql_query("SELECT * FROM `parked_location` WHERE `user_id`='" . $data['user_id'] . "'");
if (mysql_num_rows($validate) > 0) {
$query = "UPDATE `parked_location` SET `latitude`=? , `longitude`=?, `updated`=? WHERE `user_id`=?";
//$query = "INSERT INTO `parked_location` (`user_id`,`latitude`,`longitude`) VALUES (?,?,?)"; time();
$req_query = $this->mysql_bind($query, array($data['latitude'], $data['longitude'],date('Y-m-d H:i:s'),$data['user_id']));
$result = mysql_query($req_query);
if ($result) {
// $data['location_id'] = $result;
$response = array('Status' => 'success', 'Message' => 'Location saved successfully.', 'Result' => $data);
} else {
$response = array('Status' => 'failed', 'Message' => 'Error try later', 'Result' => array());
}
} else {
$query = "INSERT INTO `parked_location` (`user_id`,`latitude`,`longitude`) VALUES (?,?,?)";
$req_query = $this->mysql_bind($query, array($data['user_id'], $data['latitude'], $data['longitude']));
$result = mysql_query($req_query);
if ($result) {
// $data['location_id'] = mysql_insert_id();
$response = array('Status' => 'success', 'Message' => 'Location saved successfully.', 'Result' => $data);
} else {
$response = array('Status' => 'failed', 'Message' => 'Error try later', 'Result' => array());
}
}
} catch (Exception $ex) {
$response = array('Status' => 'failed', 'Message' => $ex->getMessage(), 'Result' => array());
}
$this->output($response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment