-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedSearchProcess.php
More file actions
306 lines (254 loc) · 10.4 KB
/
advancedSearchProcess.php
File metadata and controls
306 lines (254 loc) · 10.4 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
include "db/connections.php";
// Retrieve data from POST request
$search_txt = $_POST["t"];
$category = $_POST["cat"];
$brand = $_POST["b"];
$model = $_POST["m"];
$price_from = $_POST["pf"];
$price_to = $_POST["pt"];
$sort = $_POST["s"];
// Initialize query and status
$query = "SELECT * FROM `products`";
$status = 0;
// Build the WHERE clause based on search criteria
if (!empty($search_txt)) {
$query .= " WHERE `title` LIKE '%" . $search_txt . "%'";
$status = 1;
}
if ($category != 0 && $status == 0) {
$query .= " WHERE `category_category_id`='" . $category . "'";
$status = 1;
} elseif ($category != 0 && $status != 0) {
$query .= " AND `category_category_id`='" . $category . "'";
}
// Handle brand and model filtering
$pid = 0;
if ($brand != 0 && $model == 0) {
$model_has_brand_rs = Database::search("SELECT * FROM `model_has_brand` WHERE `brand_brand_id`='" . $brand . "'");
// Error Handling: Check if query executed successfully
if ($model_has_brand_rs === false) {
echo "Error: Query failed to execute (brand filter)";
// Handle the error, e.g., log it, display a message, etc.
exit; // Stop further execution
}
$model_has_brand_num = $model_has_brand_rs->num_rows;
for ($x = 0; $x < $model_has_brand_num; $x++) {
$model_has_brand_data = $model_has_brand_rs->fetch_assoc();
$pid = $model_has_brand_data["id"];
}
if ($status == 0) {
$query .= " WHERE `model_has_brand_id`='" . $pid . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `model_has_brand_id`='" . $pid . "'";
}
}
if ($brand == 0 && $model != 0) {
$model_has_brand_rs = Database::search("SELECT * FROM `model_has_brand` WHERE `model_model_id`='" . $model . "'");
// Error Handling: Check if query executed successfully
if ($model_has_brand_rs === false) {
echo "Error: Query failed to execute (model filter)";
// Handle the error, e.g., log it, display a message, etc.
exit; // Stop further execution
}
$model_has_brand_num = $model_has_brand_rs->num_rows;
for ($x = 0; $x < $model_has_brand_num; $x++) {
$model_has_brand_data = $model_has_brand_rs->fetch_assoc();
$pid = $model_has_brand_data["id"];
}
if ($status == 0) {
$query .= " WHERE `model_has_brand_id`='" . $pid . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `model_has_brand_id`='" . $pid . "'";
}
}
if ($brand != 0 && $model != 0) {
$model_has_brand_rs = Database::search("SELECT * FROM `model_has_brand` WHERE `brand_brand_id`='" . $brand . "' AND `model_model_id`='" . $model . "'");
// Error Handling: Check if query executed successfully
if ($model_has_brand_rs === false) {
echo "Error: Query failed to execute (brand and model filter)";
// Handle the error, e.g., log it, display a message, etc.
exit; // Stop further execution
}
$model_has_brand_num = $model_has_brand_rs->num_rows;
for ($x = 0; $x < $model_has_brand_num; $x++) {
$model_has_brand_data = $model_has_brand_rs->fetch_assoc();
$pid = $model_has_brand_data["id"];
}
if ($status == 0) {
$query .= " WHERE `model_has_brand_id`='" . $pid . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `model_has_brand_id`='" . $pid . "'";
}
}
// Apply price filtering
if (!empty($price_from) && empty($price_to)) {
if ($status == 0) {
$query .= " WHERE `price` >= '" . $price_from . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `price` >= '" . $price_from . "'";
}
}
if (empty($price_from) && !empty($price_to)) {
if ($status == 0) {
$query .= " WHERE `price` <= '" . $price_to . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `price` <= '" . $price_to . "'";
}
}
if (!empty($price_from) && !empty($price_to)) {
if ($status == 0) {
$query .= " WHERE `price` BETWEEN '" . $price_from . "' AND '" . $price_to . "'";
$status = 1;
} elseif ($status != 0) {
$query .= " AND `price` BETWEEN '" . $price_from . "' AND '" . $price_to . "'";
}
}
// Apply sorting
if ($sort == 1) {
$query .= " ORDER BY `price` ASC";
}
// Handle pagination
$pageno = isset($_POST["page"]) && $_POST["page"] != "0" ? $_POST["page"] : 1;
$results_per_page = 6;
// Execute main query (after filtering and sorting)
$product_rs = Database::search($query);
// Error Handling: Check if main query executed successfully
if ($product_rs === false) {
echo "Error: Query failed to execute (main query)";
// Handle the error, e.g., log it, display a message, etc.
exit; // Stop further execution
}
$product_num = $product_rs->num_rows;
$number_of_pages = ceil($product_num / $results_per_page);
$page_results = ($pageno - 1) * $results_per_page;
// Execute query with pagination
$selected_rs = Database::search($query . " LIMIT " . $results_per_page . " OFFSET " . $page_results . "");
// Error Handling: Check if paginated query executed successfully
if ($selected_rs === false) {
echo "Error: Query failed to execute (paginated query)";
// Handle the error, e.g., log it, display a message, etc.
exit; // Stop further execution
}
$selected_num = $selected_rs->num_rows;
// Display products
for ($x = 0; $x < $selected_num; $x++) {
$selected_data = $selected_rs->fetch_assoc();
$img_rs = Database::search("SELECT * FROM `product_images` WHERE `product_id`='" . $selected_data["id"] . "'");
// Error Handling: Check if image query executed successfully
if ($img_rs === false) {
echo "Error: Query failed to execute (image query)";
// Handle the error, e.g., log it, display a message, etc.
continue; // Skip to the next product
}
$img_data = $img_rs->fetch_assoc();
// Display product details
?>
<div class="offset-lg-1 col-12 col-lg-3">
<div class="row">
<div class="card col-6 col-lg-2 mt-2 mb-2 bi bi bi-bag text-center" style="width: 18rem;"> Rs. <?php echo $selected_data["price"]; ?> .00
<?php if ($selected_data["qty"] > 0) { ?>
<span class="badge rounded-pill text-bg-warning bi bi-newspaper">In Stock</span>
<?php } else { ?>
<span class="badge rounded-pill text-bg-danger bi bi-layout-text-sidebar">Out Of Stock</span>
<?php } ?>
<?php
$img_rs = Database::search("SELECT * FROM `product_images` WHERE `product_id`='" . $selected_data["id"] . "'");
$img_data = $img_rs->fetch_assoc();
?>
<img src="<?php echo $img_data["path"]; ?>" class="card-img-top img-thumbnail mt-2" style="height: 180px;" alt="<?php echo $selected_data["title"]; ?>">
<div class="card-body ms-0 m-0 text-center">
<h5 class="card-title fw-bold fs-6"><?php echo $selected_data["title"]; ?></h5>
<div class="d-flex justify-content-center">
</div>
<?php
if ($selected_data["qty"] > 0) {
// Check for status
if ($selected_data["status"] != 1) {
// Status is not 1, show "Add Now"
?>
<span class="card-text text-danger fw-bold">product pushed</span><br />
<span class="card-text text-danger fw-bold">not selling this item </span><br /><br />
<a href='#' class="col-12 btn btn-success disabled">
Buy Now
</a>
<?php
} else {
// Status is 1, show "Buy Now"
?>
<span class="card-text text-success fw-bold"><?php echo $selected_data["qty"]; ?> Items Available</span><br /><br />
<a href='<?php echo "singleProductView.php?id=" . ($selected_data["id"]); ?>' class="col-12 btn btn-success">
Buy Now
</a>
<?php
}
} else {
// Out of stock
?>
<span class="card-text text-danger fw-bold">Out Of Stock</span><br />
<span class="card-text text-danger fw-bold">00 Items Available</span><br /><br />
<a href='#' class="col-12 btn btn-success disabled">
Buy Now
</a>
<?php
}
?>
<!-- Modal -->
</div>
</div>
</div>
</div>
</div>
<?php
}
// Display pagination
?>
<div class="offset-2 offset-lg-3 col-8 col-lg-6 text-center mb-3">
<nav aria-label="Page navigation example">
<ul class="pagination pagination-lg justify-content-center">
<li class="page-item">
<a class="page-link" <?php if ($pageno <= 1) {
echo ("#");
} else {
?> onclick="advancedSearch(<?php echo ($pageno - 1); ?>);" ; <?php
} ?> aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php
for ($x = 1; $x <= $number_of_pages; $x++) {
if ($x == $pageno) {
?>
<li class="page-item active">
<a class="page-link" onclick="advancedSearch(<?php echo ($x); ?>);"><?php echo $x; ?></a>
</li>
<?php
} else {
?>
<li class="page-item">
<a class="page-link" onclick="advancedSearch(<?php echo ($x); ?>);"><?php echo $x; ?></a>
</li>
<?php
}
}
?>
<li class="page-item">
<a class="page-link" <?php if ($pageno >= $number_of_pages) {
echo ("#");
} else {
?> onclick="advancedSearch(<?php echo ($pageno + 1); ?>);" ; <?php
} ?> aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
<!-- </div>
</div>
</div> -->