forked from rpiambulance/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.fuel_log_entries.php
80 lines (61 loc) · 1.56 KB
/
.fuel_log_entries.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
<?php
define('URL_WITH_DASHES_ONLY', TRUE);
require_once '.db_config.php';
$connection = new PDO("mysql:host=$dhost;dbname=$dname", $duser, $dpassword);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_GET['vehicle'])) {
$vehicle = $_GET['vehicle'];
} else {
$vehicle = 'both';
}
if(isset($_GET['page']) && is_numeric($_GET['page'])) {
$page = intval($_GET['page']) - 1;
} else {
$page = 0;
}
$offset = $page * 20;
$query = <<<EOT
SELECT
f.*,
m.username,
m.first_name,
m.last_name
FROM
fuel_log f
LEFT JOIN
members m
ON
f.user = m.id
WHERE
f.date > '0000-00-00'
EOT;
if($vehicle == '5939' || $vehicle == 'FR-59') {
$query .= ' AND f.vehicle = :vehicle';
}
$query .= ' ORDER BY f.date DESC, f.time DESC';
if($offset == 0) {
$query .= ' LIMIT 20;';
} else {
$query .= ' LIMIT ' . ($offset + 1) . ', 20;';
}
$statement= $connection->prepare($query);
if($vehicle == '5939' || $vehicle == 'FR-59') {
$statement->bindValue(':vehicle', $vehicle);
}
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$query = "SELECT COUNT(*) FROM fuel_log";
if($vehicle == '5939' || $vehicle == 'FR-59') {
$query .= ' WHERE vehicle = :vehicle';
}
$statement = $connection->prepare($query);
if($vehicle == '5939' || $vehicle == 'FR-59') {
$statement->bindValue(':vehicle', $vehicle);
}
$statement->execute();
$count=$statement->fetch(PDO::FETCH_NUM)[0];
echo json_encode(array(
"total" => intval($count),
"totalPages" => ceil(intval($count)/20),
"results" => $results
));