-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.php
94 lines (77 loc) · 2.11 KB
/
filter.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
<?php
$data = [];
$i = 0;
$MaxPrice = $_POST['MaxPrice'];
$MinPrice = $_POST['MinPrice'];
$year = $_POST['year'];
$color = $_POST['color'];
$model = $_POST['model'];
$country = $_POST['country'];
$StartDate = $_POST['StartDate'];
$EndDate = $_POST['EndDate'];
$string = '';
if (!empty($MinPrice)) {
if ($string != "") {
$string = $string . "AND ";
}
$string = $string . "price_per_day>= '" . $MinPrice . "'";
}
if (!empty($MaxPrice)) {
if ($string != "") {
$string = $string . "AND ";
}
$string = $string . "price_per_day<= '" . $MaxPrice . "'";
}
if (!empty($year)) {
if ($string != "") {
$string = $string . "AND ";
}
$string = $string . "year= '" . $year . "'";
}
if (!empty($color)) {
if ($string != "") {
$string = $string . "AND ";
}
$string = $string . "color= '" . $color . "'";
}
if (!empty($model)) {
if ($string != "") {
$string = $string . "AND ";
}
$string = $string . "model= '" . $model . "'";
}
if (!empty($StartDate) && !empty($EndDate)) {
if ($string != "") {
$string = $string . "AND ";
}
$CarsInPeriod =
"
SELECT DISTINCT
car_id
FROM
reservation
WHERE ( '".$StartDate."' BETWEEN pick_date AND return_date ) OR
( '".$EndDate."' BETWEEN pick_date AND return_date)
";
$string = $string . "car_id NOT IN (".$CarsInPeriod.")";
}
$connection = mysqli_connect('localhost', 'root', '', 'car_rental_system');
if ($string != "") {
$string = "WHERE " . $string;
}
$sql = "SELECT * FROM `car` " . $string;
$query = mysqli_query($connection, $sql);
$num_rows = mysqli_num_rows($query);
if ($num_rows != 0) {
while ($row = mysqli_fetch_assoc($query)) {
$response[$i]['id'] = $row['car_id'];
$response[$i]['model'] = $row['model'];
$response[$i]['year'] = $row['year'];
$response[$i]['color'] = $row['color'];
$response[$i]['ppd'] = $row['price_per_day'];
$data[$i] = $response[$i];
$i += 1;
}
}
mysqli_close($connection);
echo json_encode($data);