-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
137 lines (86 loc) · 2.86 KB
/
update.php
File metadata and controls
137 lines (86 loc) · 2.86 KB
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
//require(__DIR__ . "/../includes/config.php");
$servername = "localhost";
$username = "root";
$password = "";
$db_name="mashup";
// Create connection
$conn = new mysqli($servername, $username, $password,$db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// ensure proper usage
if (!isset($_GET["sw"], $_GET["ne"]))
{
echo "1";
http_response_code(400);
exit;
}
// ensure each parameter is in lat,lng format
if (!preg_match("/^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$/", $_GET["sw"]) ||
!preg_match("/^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$/", $_GET["ne"]))
{
echo "2";
http_response_code(400);
exit;
}
// explode southwest corner into two variables
list($sw_lat, $sw_lng) = explode(",", $_GET["sw"]);
// explode northeast corner into two variables
list($ne_lat, $ne_lng) = explode(",", $_GET["ne"]);
/*
echo $sw_lat;
echo "<br/>";
echo $sw_lng;
echo "<br/>";
echo $ne_lat;
echo "<br/>";
echo $ne_lng;
echo "<br/>";
*/
$places = [];
// find 10 cities within view, pseudorandomly chosen if more within view
if ($sw_lng <= $ne_lng)
{
// doesn't cross the antimeridian
$sql = "SELECT * FROM mashup WHERE (" .$sw_lat .' <= latitude OR latitude <='.$ne_lat.") OR (".$sw_lat." <= longitude OR longitude <= ".$ne_lng.") GROUP BY country_code, place_name, admin_code1 ORDER BY RAND() LIMIT 10";
//echo $sql;
//$sql = "SELECT * FROM mashup "."WHERE place_name like '%".$_GET["geo"]."%'";
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
array_push($places, $row);
}
}
else
{
echo "failed";
}
}
else
{
// crosses the antimeridian
$sql = "SELECT * FROM mashup WHERE(" .$sw_lat ."<= latitude OR latitude <=".$ne_lat.") OR (".$sw_lat." <= longitude OR longitude <= ".$ne_lng.") GROUP BY country_code, place_name, admin_code1 ORDER BY RAND() LIMIT 10";
/* $rows = CS50::query("SELECT * FROM places WHERE ? <= latitude AND latitude <= ? AND (? <= longitude OR longitude <= ?) GROUP_BY country_code, place_name, admin_code1 ORDER BY RAND() LIMIT 10", $sw_lat, $ne_lat, $sw_lng, $ne_lng);
*/
//echo $sql;
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
array_push($places, $row);
}
}
else
{
echo "failed";
}
}
// output places as JSON (pretty-printed for debugging convenience)
header("Content-type: application/json");
print(json_encode($places, JSON_PRETTY_PRINT));
?>