-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowse.php
More file actions
376 lines (314 loc) · 15.9 KB
/
browse.php
File metadata and controls
376 lines (314 loc) · 15.9 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
require_once "config.php";
require_once "send_mail.php";
include_once("header.php");
require("utilities.php");
?>
<div class="container">
<h2 class="my-3">Browse listings</h2>
<div id="searchSpecs">
<!-- When this form is submitted, this PHP page is what processes it.
Search/sort specs are passed to this page through parameters in the URL
(GET method of passing data to a page). -->
<form method="get" action="browse.php">
<div class="row">
<div class="col-md-5 pr-0">
<div class="form-group">
<label for="keyword" class="sr-only">Search keyword:</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text bg-transparent pr-0 text-muted">
<i class="fa fa-search"></i>
</span>
</div>
<input type="text" class="form-control border-left-0" name="keyword" id="keyword" placeholder="Search for anything">
</div>
</div>
</div>
<div class="col-md-3 pr-0">
<div class="form-group">
<label for="cat" class="sr-only">Search within:</label>
<select class="form-control" name="cat" id="cat">
<option selected value="all">All categories</option>
<option value="jewellery">Jewellery </option>
<option value="art_works">Art Works </option>
<option value="electronics">Electronics </option>
<option value="books">Books </option>
<option value="homes">Homes </option>
</select>
</div>
</div>
<div class="col-md-3 pr-0">
<div class="form-inline">
<label class="mx-2" for="order_by">Sort by:</label>
<select class="form-control" name="order_by" id="order_by">
<option selected value="pricelow">Price (low to high)</option>
<option value="pricehigh">Price (high to low)</option>
<option value="date">Soonest expiry</option>
</select>
</div>
</div>
<div class="col-md-1 px-0">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</form>
</div> <!-- end search specs bar -->
</div>
<?php
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "SELECT * FROM item WHERE status = '0'";
$result = $link->query($sql);
$now = new DateTime();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$end_time = new DateTime($row["end_date"]);
$reserve_price = $row["reserve_price"];
$current_price = $row["current_price"];
$id = $row["item_id"];
$seller_id = $row["seller_id"];
$buyer_id = $row["buyer_id"];
$title = $row["title"];
if ($now > $end_time) {
if ($current_price>$reserve_price){
$check = "UPDATE item SET status = '1' WHERE item_id = '$id'";
// Send email to seller after their item sold.
$seller_emails = "SELECT email FROM user WHERE user_id = '$seller_id'";
$email_result = $link->query($seller_emails);
while ($row = mysqli_fetch_array($email_result)) {
$seller_email = $row['email'];
$subject = "Item Sold";
$body = "Hi there, <br/> <br/> Your ".title." has already sold. <br/> <br/> Kind regards, <br/> Simple Click Marketing Team <br/>";
send_email($seller_email, $subject, $body);
}
// Send email to buyer after they won the auction.
$buyer_emails = "SELECT email FROM user WHERE user_id = '$buyer_id'";
$result = $link->query($buyer_emails);
while ($row = mysqli_fetch_array($result)) {
$buyer_email = $row['email'];
$subject = "You Won The Auction";
$body = "Hi there, <br/> <br/> You won the auction on ".$title."!<br/> <br/> Kind regards, <br/> Simple Click Marketing Team <br/>";
send_email($buyer_email, $subject, $body);
}
}
else {
$check = "UPDATE item SET status = '2' WHERE item_id = '$id'";
// Send email to seller after the price of item didn't reach the reserve price.
$Seller_emails = "SELECT email FROM user WHERE user_id = '$seller_id'";
$email_result = $link->query($Seller_emails);
while ($row = mysqli_fetch_array($email_result)) {
$seller_email = $row['email'];
$subject = "The price of your ".$title."did not reach the reserved price";
$body = "Hi there, <br/> <br/> Your ".$title." did not reach the reserved price. <br/> <br/> Kind regards, <br/> Simple Click Marketing Team <br/>";
send_email($seller_email, $subject, $body);
}
}
mysqli_query($link,$check);
}
}
} else {
echo " ";
}
?>
<?php
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql_total = "SELECT * FROM item";
$rs_result = $link->query($sql_total);
[$max_page,$results_per_page,$start_from] = page_calculation($rs_result);
// Retrieve these from the URL
if (!isset($_GET['keyword'])) {
$keyword = '';
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
}
else {
$keyword = $_GET['keyword'];
}
if (!isset($_GET['cat'])) {
$category = '';
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
}
else {
$category = $_GET['cat'];
}
if (!isset($_GET['order_by'])) {
$ordering = '';
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
}
else {
$ordering = $_GET['order_by'];
if ($ordering == "date")
{
if ($category == 'all') {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')) AND (status = '0')
ORDER BY end_date
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')) AND (status = '0')";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
else {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE (category='$category') AND (status = '0') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))
ORDER BY end_date
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE (category='$category') AND (status = '0') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
}
elseif ($ordering == "pricelow")
{
if ($category == 'all') {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE (title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')
ORDER BY LENGTH(current_price), current_price
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE (title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
else {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE (category='$category') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))
ORDER BY LENGTH(current_price), current_price
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE (category='$category') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
}
elseif ($ordering == "pricehigh")
{
if ($category == 'all') {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE (title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')
ORDER BY LENGTH(current_price) DESC, current_price DESC
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE (title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%')";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
else {
$sql = "SELECT item_id, title, description, current_price, num_bids, end_date FROM item
WHERE (category='$category') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))
ORDER BY LENGTH(current_price) DESC, current_price DESC
LIMIT $start_from,$results_per_page";
$result = $link->query($sql);
$data = "SELECT * FROM item WHERE (category='$category') AND ((title LIKE '%$keyword%') OR (category LIKE '%$keyword%') OR (description LIKE '%$keyword%'))";
$data_result = $link ->query($data);
$num_results = mysqli_num_rows($data_result);
$max_page = ceil($num_results / $results_per_page);
}
}
}
if (!isset($_GET['page'])) {
$curr_page = 1;
}
else {
$curr_page = $_GET['page'];
}
?>
<div class="container mt-5">
<!-- TODO: If result set is empty, print an informative message. Otherwise... -->
<ul class="list-group">
<!-- TODO: Use a while loop to print a list item for each auction listing
retrieved from the query -->
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$list_id = $row["item_id"];
$title = $row["title"];
$description = $row["description"];
$current_price = $row["current_price"];
$num_bids = $row["num_bids"];
try {
$end_time = new DateTime($row["end_date"]);
} catch (Exception $e) {
}
$item_id = $row["item_id"];
print_listing_li($item_id, $title, $description, $current_price, $num_bids, $end_time);
}
} else {
echo "0 results";
}
?>
</ul>
<!-- Pagination for results listings -->
<nav aria-label="Search results pages" class="mt-5">
<ul class="pagination justify-content-center">
<?php
// Copy any currently-set GET variables to the URL.
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") {
$querystring .= "$key=$value&";
}
}
$high_page_boost = max(3 - $curr_page, 0);
$low_page_boost = max(2 - ($max_page - $curr_page), 0);
$low_page = max(1, $curr_page - 2 - $low_page_boost);
$high_page = min($max_page, $curr_page + 2 + $high_page_boost);
if ($curr_page != 1) {
echo('
<li class="page-item">
<a class="page-link" href="browse.php?' . $querystring . 'page=' . ($curr_page - 1) . '" aria-label="Previous">
<span aria-hidden="true"><i class="fa fa-arrow-left"></i></span>
<span class="sr-only">Previous</span>
</a>
</li>');
}
for ($i = $low_page; $i <= $high_page; $i++) {
if ($i == $curr_page) {
// Highlight the link
echo('
<li class="page-item active">');
}
else {
// Non-highlighted link
echo('
<li class="page-item">');
}
// Do this in any case
echo('
<a class="page-link" href="browse.php?' . $querystring . 'page=' . $i . '">' . $i . '</a>
</li>');
}
if ($curr_page != $max_page) {
echo('
<li class="page-item">
<a class="page-link" href="browse.php?' . $querystring . 'page=' . ($curr_page + 1) . '" aria-label="Next">
<span aria-hidden="true"><i class="fa fa-arrow-right"></i></span>
<span class="sr-only">Next</span>
</a>
</li>');
}
$link->close();
?>
</ul>
</nav>
</div>
<?php include_once("footer.php")?>