-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.php
137 lines (101 loc) · 3.98 KB
/
Search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
if (!empty($_POST)) {
$long = "";
$lat = "";
$searchQuery = "";
if (empty($_POST["search"]) ||empty($_POST["longitude"]) || empty($_POST["latitude"])) {
$response["success"] = 0;
$response["message"] = "Search query or Location was empty.";
die(json_encode($response));
} else{
$searchQuery = trim($_POST['search']);
$long = $_POST['longitude'];
$lat = $_POST["latitude"];
}
}else{
$response["success"] = 0;
$response["message"] = "Post data was empty.";
die(json_decode($response));
}
$con = mysqli_connect("localhost", "root", "WhatHappendPWD.", "whathappend");
if(mysqli_connect_error()){
echo "failed to connect".mysqli_connect_error();
}
//For differnet query depending upon whether the search query was just a hashtag or a string.
if(strpos(trim($txt), '#') == 0 && strpos(trim($txt), ' ') == FALSE)
{
//Search for hashtag
$query = "SELECT `id`, `post`, `user`, `long`, `lat`, `range`, `timestamp` FROM posts WHERE UPPER(`post`) LIKE '%$searchQuery%' ORDER BY id DESC";
}
else
{
//Search for a string
$query = "SELECT `id`, `post`, `user`, `long`, `lat`, `range`, `timestamp` FROM posts WHERE MATCH (post) AGAINST ('%$searchQuery%' IN NATURAL LANGUAGE MODE) ORDER BY id DESC";
}
if ($result = $con->query($query)) {
$response["success"] = 1;
$response["message"] = "Posts available";
$response["posts"] = array();
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$distance = vincentyGreatCircleDistance($long, $lat, $row["long"], $row["lat"]);
if($distance < $row["range"]){
$post = array();
$post["id"] = $row["id"];
$post["username"] = $row["user"];
$post["post"] = $row["post"];
$post["longitude"] = $row["long"];
$post["latitude"] = $row["lat"];
$post["timestamp"] = $row["timestamp"];
$post["distance"] = $distance;
//update our repsonse JSON data
array_push($response["posts"], $post);
}
}
$result->free();
//Check if user wants to sort by distance or timestamp
if(!empty($_POST["sortByDistance"]) && $_POST["sortByDistance"]){
usort($response["posts"], function($a, $b) {
return $a["distance"] - $b["distance"];
});
}
//Loop through all the posts and convert meters into kilometers if its more than 1000 meters
foreach($response["posts"] as &$dis){
if($dis["distance"] >= 1000){
$dis["distance"] = $dis["distance"]/1000;
$dis["distance"] = number_format($dis["distance"], 2, '.', ',')." Km";
}else{
$dis["distance"] = number_format($dis["distance"])." m";
}
}
// echoing JSON response
echo json_encode($response);
}
//Disconnect from the server
mysqli_close($con);
/**
* Calculates the great-circle distance between two points, with
* the Vincenty formula.
* @param float $latitudeFrom Latitude of start point in [deg decimal]
* @param float $longitudeFrom Longitude of start point in [deg decimal]
* @param float $latitudeTo Latitude of target point in [deg decimal]
* @param float $longitudeTo Longitude of target point in [deg decimal]
* @param float $earthRadius Mean earth radius in [m]
* @return float Distance between points in [m] (same as earthRadius)
*/
function vincentyGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) +
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
$angle = atan2(sqrt($a), $b);
return $angle * $earthRadius;
}
?>