-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookings.php
164 lines (149 loc) · 5.95 KB
/
bookings.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
<?php
$select_reservations = <<<EOT
SELECT
r.passenger AS passenger,
start.name AS start,
s.departure AS departing,
end.name AS end,
eta(s.departure, train.speed, track.distance) AS eta,
train.name AS train
FROM
reservation AS r
LEFT JOIN schedule AS s ON s.id = r.schedule
LEFT JOIN train AS train ON train.id = s.train
LEFT JOIN track AS track ON track.id = s.track
LEFT JOIN station AS start ON start.id = track.start
LEFT JOIN station AS end ON end.id = track.end
ORDER BY s.departure, r.passenger ASC
EOT;
$conn = new mysqli("localhost", "csci601", "csci601", "csci601");
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
$station_options = "";
$result = $conn->query("SELECT id, name FROM station ORDER BY name ASC");
while ( $row = $result->fetch_assoc() ) {
$station_options .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Book A Choooo Chooooo</title>
<style>
body {
font-family: "Courier New", monospace;
font-size: small;
}
.insert {
padding: 5px;
border-style: solid;
border-width: 1px;
margin-bottom: 5px;
}
.insert p {
font-weight: bold;
color: red;
}
.insert form {
margin-bottom: 5px;
}
.insert th {
color: red;
}
th, td {
padding: 3px;
text-align: center;
border-bottom: 1px solid #bbb;
}
td {
text-align: left;
}
</style>
</head>
<body>
<div>
<a href='index.php'>home</a>
</div>
<div class="insert">
<p>Book</p>
<form action="book_travel.php" method="post">
<label> Passenger: <input type="text" name="book_name" /></label>
<label> Date: <input type="text" name="book_date" /></label>
<label> From: <select name='book_from'><?php echo $station_options; ?></select></label>
<label> To: <select name='book_to'><?php echo $station_options; ?></select></label>
<input type="submit" />
</form>
<table>
<thead><tr><th>Passenger</th><th>From</th><th>Departing</th><th>To</th><th>Arriving</th><th>Train</th></tr></thead>
<tbody>
<?php
$result = $conn->query($select_reservations);
if (!$result) {
die("Failed to query reservations: " . $conn->error);
}
while ($row = $result->fetch_assoc()) {
echo '<tr><td>' . $row['passenger'] . '</td><td>' . $row['start'] . '</td><td>'
. $row['departing'] . '</td><td>' . $row['end'] . '</td><td>' . $row['eta']
. '</td><td>' . $row['train'] . '</td></tr>';
}
?>
</tbody>
</table>
</div>
<div class="insert">
<p>Reservations By Passenger</p>
<?php
$resv = array();
$result = $conn->query("SELECT id, passenger FROM reservation GROUP BY id, passenger ORDER BY passenger ASC");
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$pass = $row['passenger'];
if (!isset($resv[$pass])) {
$resv[$pass] = array();
}
array_push($resv[$pass], $id);
}
$result->close();
$lookup = $conn->prepare(
<<<EOT
SELECT
s.departure AS departure,
train.name AS train,
start.name AS start,
end.name AS end,
eta(s.departure, train.speed, track.distance)
FROM
reservation AS r
LEFT JOIN schedule AS s ON s.id = r.schedule
LEFT JOIN train ON train.id = s.train
LEFT JOIN track ON track.id = s.track
LEFT JOIN station AS start ON start.id = track.start
LEFT JOIN station as end ON end.id = track.end
WHERE
r.id = ? ORDER BY s.departure ASC
EOT
);
foreach ($resv as $pass => $ids) {
echo '<div class="insert"><p>' . $pass . '</p>';
foreach ($ids as $id) {
$lookup->bind_param("s", $id);
$lookup->execute();
$lookup->bind_result($date, $train, $start, $end, $eta);
echo '<table><tbody><tr><td>';
echo '<table class="reservation"><thead><tr><th>Departing Time</th><th>Leaving</th><th>Arriving</th><th>ETA</th><th>Train</th></tr></thead><tbody>';
while ($lookup->fetch()) {
echo '<tr><td>' . $date . '</td><td>' . $start . '</td><td>' . $end . '</td><td>' . $eta . '</td><td>' . $train . '</td></tr>';
}
echo '</tbody></table></td><td>';
echo '<form method="POST" action="cancel_reservation.php"><input type="hidden" name="id" value="' . $id . '" /><input type="submit" value="Cancel Trip" /></form>';
echo '</td></tr></tbody></table>';
$lookup->reset();
}
echo '</div>';
}
$lookup->close();
?>
</body>
</html>