-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_cars.php
187 lines (178 loc) · 8.13 KB
/
display_cars.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
<?php
require "components/connection.php";
// the code here is for the filter form, the if condition checks whether the user has clicked clear filters button
if (isset($_POST["clear"])) {
$sql = "SELECT * from cars WHERE vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
}
// the code here is for the filter form, if the user fills filter form then 6 conditions arises, each of them has different SQL query
elseif (isset($_POST["submit"])) {
$rent = $_POST["rent"];
$seating_capacity = $_POST["seating_capacity"];
if ($seating_capacity == null && $rent == "low") {
$sql = "SELECT * from cars WHERE rent < 1000 && vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
} elseif ($seating_capacity == null && $rent == "high") {
$sql = "SELECT * from cars WHERE rent > 1000 && vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
} elseif ($seating_capacity != null && $rent == "all") {
$sql = "SELECT * from cars WHERE seating_capacity = :seating_capacity && vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->bindParam(
"seating_capacity",
$seating_capacity,
PDO::PARAM_STR
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
} elseif ($seating_capacity != null && $rent == "low") {
$sql = "SELECT * from cars WHERE seating_capacity = :seating_capacity && rent<1000 && vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->bindParam(
"seating_capacity",
$seating_capacity,
PDO::PARAM_STR
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
} elseif ($seating_capacity != null && $rent == "high") {
$sql = "SELECT * from cars WHERE seating_capacity = :seating_capacity && rent>1000 && vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->bindParam(
"seating_capacity",
$seating_capacity,
PDO::PARAM_STR
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
}
//$seating_capacity == null && $rent == "all"
else {
$sql = "SELECT * from cars WHERE vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
}
}
//this condition runs when the page loads for the first time without use of filters, the default SQL statemet to print all cars is used here
else {
$sql = "SELECT * from cars WHERE vehicle_number NOT IN (SELECT vehicle_number from rent)";
$query = $con->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Available cars</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- css -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/tableDisplay.css">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous">
</script>
<!-- fonts -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
<!-- favicon link -->
<link rel = "icon" href = "img/favicon.png" type = "image/x-icon">
</head>
<body>
<!-- navigation bar -->
<?php include "components/header.php"; ?>
<!-- Below this is the code for filtering results -->
<div class="search">
<form name="display_cars" method="post" id="fileForm" role="form" class="row gy-2 gx-3 align-items-center" required>
<div class="col-md-4 col-md-offset-2">
<label class="visually-hidden" for="rent">Preference
</label>
<select name="rent" class="form-select" id="rent" required>
<option value="all">Charges (ALL)
</option>
<option value="low">Upto ₹1000
</option>
<option value="high">More than ₹1000
</option>
</select>
</div>
<div class="col-md-4 col-md-offset-2">
<input class="form-control" type="number" name="seating_capacity" id="seating_capacity" placeholder="Seating capacity (Leave blank for all)" value=null min="1" max="20">
</div>
<div class="col-md-3 col-md-offset-2">
<input class="btn btn-success" type="submit" name="submit" value="Search">
<input class="btn btn-success" type="submit" name="clear" value="Clear">
</div>
</form>
</div>
<!-- till here is the filter results code -->
<!-- now below here is the code for table diaplay -->
<br>
<hr>
<br>
<div class="container">
<div class= "home_display">
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>
<b>Available Cars to Rent
</b>
</h3>
<hr />
<div class="table-responsive">
<br>
<table id="mytable" class="table table-bordred table-striped">
<thead>
<th>#</th>
<th>Vehicle Number</th>
<th>Model</th>
<th>Seating Capacity</th>
<th>Charges(per day)</th>
<th>Action</th>
</thead>
<tbody>
<?php
$cnt = 1;
if ($query->rowCount() > 0) {
//In case that the query returned at least one record, we can echo the records within a foreach loop
foreach ($results as $result) { ?>
<tr>
<td><?php echo htmlentities($cnt); ?></td>
<td><?php echo htmlentities($result->vehicle_number); ?></td>
<td><?php echo htmlentities($result->model); ?></td>
<td><?php echo htmlentities($result->seating_capacity) . " people"; ?></td>
<td><?php echo "₹ " . htmlentities($result->rent); ?></td>
<td style="text-align: center;">
<a href="customers/rent_car.php?vehicle_number=<?php echo htmlentities($result->vehicle_number); ?>">
<button type="button" class="btn btn-success btn-sm">Rent</button>
</a>
</td>
</tr>
<?php $cnt++;}
} else {
echo "<strong>No cars available!</strong><div>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include "components/footer.html"; ?>
</body>
</html>