-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetch_incidents.php
More file actions
51 lines (44 loc) · 1.57 KB
/
fetch_incidents.php
File metadata and controls
51 lines (44 loc) · 1.57 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
<?php
header('Content-Type: application/json');
require_once 'mysqli_db.php';
session_start();
if (!isset($_SESSION['user_id'])) {
die('User not logged in');
}
$sql = "SELECT i.*, u.first_name, u.last_name FROM incidents i
LEFT JOIN users u ON i.user_id = u.id
WHERE i.status='pending' ORDER BY i.created_at DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$incidents = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Get coordinates based on location name from lpuLocations
$location = strtolower($row['location']);
$sql_locations = "SELECT name, latitude, longitude FROM locations WHERE is_active = TRUE";
$result_locations = $conn->query($sql_locations);
$coordinates = [];
if ($result_locations->num_rows > 0) {
while($loc = $result_locations->fetch_assoc()) {
$coordinates[strtolower($loc['name'])] = ['lat' => (float)$loc['latitude'], 'lng' => (float)$loc['longitude']];
}
}
foreach ($coordinates as $key => $coord) {
if (strpos($location, $key) !== false) {
$row['latitude'] = $coord['lat'];
$row['longitude'] = $coord['lng'];
break;
}
}
// If no match found, use a default location
if (!isset($row['latitude'])) {
$row['latitude'] = 31.2533; // Default to Block 32
$row['longitude'] = 75.7050;
}
$incidents[] = $row;
}
}
echo json_encode($incidents);
$conn->close();
?>