-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
233 lines (209 loc) · 7.95 KB
/
db.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
class connectToDB
{
private $conn;
public function __construct()
{
$config = include 'config.php';
$this->conn = new mysqli( $config['db']['server'], $config['db']['user'], $config['db']['pass'], $config['db']['dbname']);
// var_dump($config, $this->conn);
}
function __destruct()
{
$this->conn->close();
}
public function addCompany( $company, $details, $latitude, $longitude, $telephone, $keywords)
{
$statement = $this->conn->prepare("INSERT INTO companies( company, details, latitude, longitude, telephone, keywords) VALUES( ?, ?, ?, ?, ?, ?)");
$statement->bind_param('ssssss', $company, $details, $latitude, $longitude, $telephone, $keywords);
$statement->execute();
$statement->close();
$this->conn->close();
}
public function getCompaniesList()
{
$arr = array();
$statement = $this->conn->prepare( "SELECT id, company, details, latitude, longitude, telephone, keywords from companies order by company ASC");
$statement->bind_result( $id, $company, $details, $latitude, $longitude, $telephone, $keywords);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "company" => $company, "details" => $details, "latitude" => $latitude, "longitude" => $longitude, "telephone" => $telephone, "keywords" => $keywords];
}
$statement->close();
return $arr;
}
public function updateCompany( $id, $details, $latitude, $longitude, $telephone, $keywords)
{
$statement = $this->conn->prepare("UPDATE companies SET details = ?,latitude = ?,longitude = ?,telephone = ?,keywords = ? where id = ?");
$statement->bind_param( 'sssssi', $details, $latitude, $longitude, $telephone, $keywords, $id);
$statement->execute();
$statement->close();
}
public function deleteCompany($id)
{
$statement = $this->conn->prepare("Delete from companies where id = ?");
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
}
public function addStreet( $street, $geo, $keywords)
{
$statement = $this->conn->prepare("INSERT INTO streets( name, geolocations, keywords) VALUES( ?, ?, ?)");
$statement->bind_param( 'sss', $street, $geo, $keywords);
$statement->execute();
$statement->close();
}
public function getStreetsList()
{
$arr = array();
$statement = $this->conn->prepare( "SELECT id, name, geolocations, keywords from streets order by name ASC");
$statement->bind_result( $id, $name, $geolocations, $keywords);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "name" => $name, "geolocations" => $geolocations, "keywords" => $keywords];
}
$statement->close();
return $arr;
}
public function updateStreet( $id, $geo, $keywords)
{
$statement = $this->conn->prepare( "UPDATE streets SET geolocations = ?, keywords = ? where id = ?");
$statement->bind_param( 'ssi', $geo, $keywords, $id);
$statement->execute();
$statement->close();
}
public function deleteStreet($id)
{
$statement = $this->conn->prepare("Delete from streets where id = ?");
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
}
public function addArea( $area, $geo, $keywords)
{
$statement = $this->conn->prepare( "INSERT INTO areas( name, geolocations, keywords ) VALUES(?,?,?)");
$statement->bind_param( 'sss', $area, $geo,$keywords);
$statement->execute();
$statement->close();
}
public function getAreasList()
{
$arr = array();
$statement = $this->conn->prepare( "SELECT id, name, geolocations, keywords from areas order by name ASC");
$statement->bind_result( $id, $name, $geolocations, $keywords);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "name" => $name, "geolocations" => $geolocations, "keywords" => $keywords];
}
$statement->close();
return $arr;
}
public function updateArea( $id, $geo, $keywords)
{
$statement = $this->conn->prepare("UPDATE areas SET geolocations = ?, keywords = ? where id = ?");
$statement->bind_param( 'ssi', $geo, $keywords, $id);
$statement->execute();
$statement->close();
}
public function deleteArea($id)
{
$statement = $this->conn->prepare("Delete from areas where id = ?");
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
}
public function getSearchResults($keyword)
{
$arr = array();
$jsonData = '{"results":[';
$this->conn->query( "SET NAMES 'UTF8'" );
$statement = $this->conn->prepare("SELECT company, latitude, longitude FROM `companies` where keywords REGEXP ? or company REGEXP ?");
$statement->bind_param( 'ss', $keyword, $keyword);
$statement->execute();
$statement->bind_result( $name, $lat, $lng);
while ($statement->fetch()) {
$arr[] = '{"name":"' . $name. '","latitude":"' . $lat. '","longitude":"' . $lng. '"}';
}
$statement->close();
$statement = $this->conn->prepare( "SELECT name, geolocations FROM `streets` where keywords REGEXP ? or name REGEXP ?");
$statement->bind_param( 'ss', $keyword, $keyword);
$statement->execute();
$statement->bind_result( $name, $geolocations);
while ($statement->fetch()) {
$temp = explode(",",$geolocations);
$arr[] = '{"name":"' . $name. '","latitude":"' . $temp[1]. '","longitude":"' . $temp[0]. '"}';
}
$statement->close();
$statement = $this->conn->prepare( "SELECT name, geolocations FROM `areas` where keywords REGEXP ? or name REGEXP ?");
$statement->bind_param( 'ss', $keyword, $keyword);
$statement->execute();
$statement->bind_result( $name, $geolocations);
while ($statement->fetch()) {
$temp = explode(",",$geolocations);
$arr[] = '{"name": "' . $name. '", "latitude": "' . $temp[1]. '","longitude":"' . $temp[0]. '"}';
}
$statement->close();
$jsonData .= implode(",", $arr);
$jsonData .= ']}';
return $jsonData;
}
public function getCompaniesList() {
$arr = array();
$statement = $this->conn->prepare("Select id, company, details, latitude, longitude, telephone from companies order by company ASC");
$statement->bind_result( $id, $company, $details, $latitude, $longitude, $telephone);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "company" => $company, "details" => $details, "latitude" => $latitude, "longitude" => $longitude, "telephone" => $telephone];
}
$statement->close();
return $arr;
}
public function addStreet($street,$geo) {
$statement = $this->conn->prepare("Insert INTO streets( name, geolocations ) VALUES(?,?)");
$statement->bind_param('ss', $street, $geo);
$statement->execute();
$statement->close();
}
public function updateStreet($id, $geo) {
$statement = $this->conn->prepare("Update streets SET geolocations = ? where id = ?");
$statement->bind_param('si', $geo, $id);
$statement->execute();
$statement->close();
}
public function deleteStreet($id) {
$statement = $this->conn->prepare("Delete from streets where id = ?");
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
}
public function addArea($area,$geo) {
$statement = $this->conn->prepare("Insert INTO areas( name, geolocations ) VALUES(?,?)");
$statement->bind_param('ss', $area, $geo);
$statement->execute();
$statement->close();
}
public function getAreasList() {
$arr = array();
$statement = $this->conn->prepare("Select id, name, geolocations from areas order by name ASC");
$statement->bind_result( $id, $name, $geolocations);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "name" => $name, "geolocations" => $geolocations];
}
$statement->close();
return $arr;
}
public function updateArea($id,$geo) {
$statement = $this->conn->prepare("Update areas SET geolocations = ? where id = ?");
$statement->bind_param('si', $geo, $id);
$statement->execute();
$statement->close();
}
public function deleteArea($id) {
$statement = $this->conn->prepare("Delete from areas where id = ?");
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
}
}
$conn = new connectToDB();