-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.php
256 lines (231 loc) Β· 11.2 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<div class="h-100 mt-4 filter-container px-1 bg-info bg-opacity-25">
<div>
<div class="filters-header">
<div class="heading">Filter</div>
<button onclick="resetfields()" class="btn btn-sm radius-lg filter-reset-button">Reset</button>
</div>
<div class="sub-filter-heading">
<div>Facilities</div>
</div>
<div class="amenities">
<?php
$sql = "select * from amenities";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
for ($i=0; $i < $num; $i++) {
$row = mysqli_fetch_assoc($result);
$amenName = $row['name'];
$newText = str_replace(" ", "", $amenName);
$amenId = $row['id'];
echo '<div class="checkbox-filter">
<label for="filter_facility_pool'.$i.'" class="custom-checkbox">
<input type="checkbox" name='.$newText.' id="filter_facility_pool'.$i.'" value="'.$amenName.'">
<span class="custom-checkbox-icon"></span>
'.$amenName.'
</label>
</div>';
}
?>
</div>
<div class="sub-filter-heading">
<div>Locations</div>
</div>
<div class="location">
<div class="search-bar">
<input type="text" oninput="locationSuggest()" id="location" class="w-100" placeholder="Enter Location">
<ul id="location-suggestion"></ul>
</div>
<div class="mt-2" id="location-list">
</div>
</div>
<div class="sub-filter-heading">
<div>Rating</div>
</div>
<div class="ratings">
<div class="checkbox-filter">
<label for="filter_rating_5" class="custom-checkbox">
<input type="checkbox" name="5" id="filter_rating_5" value="5">
<span class="custom-checkbox-icon"></span>
5 Stars
</label>
</div>
<div class="checkbox-filter">
<label for="filter_rating_4" class="custom-checkbox">
<input type="checkbox" name="4" id="filter_rating_4" value="4">
<span class="custom-checkbox-icon"></span>
4 Stars
</label>
</div>
<div class="checkbox-filter">
<label for="filter_rating_3" class="custom-checkbox">
<input type="checkbox" name="3" id="filter_rating_3" value="3">
<span class="custom-checkbox-icon"></span>
3 Stars
</label>
</div>
<div class="checkbox-filter">
<label for="filter_rating_2" class="custom-checkbox">
<input type="checkbox" name="2" id="filter_rating_2" value="2">
<span class="custom-checkbox-icon"></span>
2 Stars
</label>
</div>
<div class="checkbox-filter">
<label for="filter_rating_1" class="custom-checkbox">
<input type="checkbox" name="1" id="filter_rating_1" value="1">
<span class="custom-checkbox-icon"></span>
1 Star
</label>
</div>
</div>
<div class="sub-filter-heading">
<div>Duration</div>
</div>
<div>
<div class="checkbox-filter">
<label for="filter_duration_0_2hours" class="custom-checkbox">
<input type="checkbox" name="durations" id="filter_duration_0_2hours" value="0_2hours">
<span class="custom-checkbox-icon"></span>
0-2 Hours
</label>
</div>
<div class="checkbox-filter">
<label for="filter_duration_2_5hours" class="custom-checkbox">
<input type="checkbox" name="durations" id="filter_duration_2_5hours" value="2_5hours">
<span class="custom-checkbox-icon"></span>
2-5 Hours
</label>
</div>
<div class="checkbox-filter">
<label for="filter_duration_full_day" class="custom-checkbox">
<input type="checkbox" name="durations" id="filter_duration_full_day" value="full_day">
<span class="custom-checkbox-icon"></span>
Full Day
</label>
</div>
<div class="checkbox-filter">
<label for="filter_duration_multi_day" class="custom-checkbox">
<input type="checkbox" name="durations" id="filter_duration_multi_day" value="multi_day">
<span class="custom-checkbox-icon"></span>
Multi Day
</label>
</div>
</div>
</div>
<div>
<button onclick="applyFilter()" class="btn btn-sm radius-lg filter-reset-button mx-auto">Apply</button>
</div>
</div>
<script>
const ratings = document.getElementsByClassName('ratings')
const ratingFields = ratings[0].getElementsByTagName('input');
const amenities = document.getElementsByClassName('amenities')
const amenFields = amenities[0].getElementsByTagName('input');
const locations = document.getElementsByClassName('search-bar')
const locaFields = locations[0].getElementsByTagName('input');
const suggestionList = document.getElementById("location-suggestion");
const suggestionSaved = document.getElementById("location-list");
const savedLocation = document.getElementsByClassName("saved-location");
for (let index = 0; index < ratingFields.length; index++) {
const element = ratingFields[index];
element.addEventListener('change', ()=>{
for (let index2 = 0; index2 < ratingFields.length; index2++) {
const otherBox = ratingFields[index2];
if(otherBox !== element){
otherBox.checked = false
}
}
})
}
function resetfields(event) {
// event.preventDefault();
let parentCheckForm = document.getElementsByClassName('filter-container')
let checkFields = parentCheckForm[0].getElementsByTagName('input')
for (let index = 0; index < checkFields.length; index++) {
const element = checkFields[index];
element.checked = false
}
}
function applyFilter() {
let filterUrl = "./search.php?"
let filterIds = []
let count = 0
// checking filters for amenities
for (let index = 0; index < amenFields.length; index++) {
const element = amenFields[index];
if(element.checked){
filterIds[count] = `${element.value}`
count++;
}
}
if(count){
filterUrl += `&amenities=${filterIds.join(',')}`
}
// checking filters for ratings
for (let index = 0; index < ratingFields.length; index++) {
const element = ratingFields[index];
if(element.checked){
filterUrl += `&rating=${element.value}`
break
}
}
// checking for locations
let locationVal = ""
let value = ""
// console.log(savedLocation.length)
for (let index = 0; index < savedLocation.length-1; index++) {
const element = savedLocation[index];
value = element.textContent.trim();
// console.log(value)
if (value !== "") {
locationVal += value
locationVal += '.'
}
}
if (savedLocation.length) {
locationVal += savedLocation[savedLocation.length-1].textContent.trim()
// console.log(value)
filterUrl += `&location=${locationVal}`
}
// console.log(locationVal)
window.location = filterUrl
}
async function locationSuggest() {
let locationVal = locaFields[0].value.trim()
let suggestions = await fetch(`http://localhost/WT-Project/Partials/locationSug.php?location=${locationVal}`)
suggestions = await suggestions.json()
suggestionList.innerHTML = '';
if (suggestions.length > 0) {
suggestionList.style.display = "block";
suggestions.forEach(suggestion => {
const listItem = document.createElement("li");
listItem.textContent = suggestion.location;
suggestionList.appendChild(listItem);
listItem.addEventListener("click", function() {
// When a suggestion is clicked, fill the input field with the suggestion and hide the suggestion list
locaFields[0].value = suggestion.location;
suggestionList.style.display = "none";
let addsuggestionSaved = document.createElement("div");
addsuggestionSaved.innerHTML = `<p class="py-1 px-3 my-2 bg-warning rounded-pill">
<span class="saved-location">${suggestion.location}</span>
<svg xmlns="http://www.w3.org/2000/svg" onclick="removeLocation(this)" width="16" height="16" fill="currentColor" class="my-1 bi bi-x-circle" viewBox="0 0 16 16" style="float:right;cursor:pointer;">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
<path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
</svg>
</p>`;
suggestionSaved.appendChild(addsuggestionSaved);
});
});
} else {
suggestionList.style.display = "none";
}
}
document.addEventListener("click", function(event) {
if (event.target !== locaFields[0]) {
suggestionList.style.display = "none";
}
});
function removeLocation(element) {
suggestionSaved.removeChild(element.parentNode.parentNode)
}
</script>