-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse.php
258 lines (217 loc) · 7.3 KB
/
browse.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
257
258
<?php include_once("header.php")?>
<?php require("utilities.php")?>
<?php require("database.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" id="keyword" placeholder="Search for anything" name="keyword">
</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" id="cat" name="cat">
<option selected value="a.Category">All categories</option>
<?php
// the code to populate the category list -- Donald
$querryCategoryList = "SELECT Category FROM auction.categorylist ORDER BY Category ASC";
$resultCatrgory = mysqli_query($connectionView, $querryCategoryList);
while ($rowCategory = mysqli_fetch_array($resultCatrgory))
{
echo "<option value='".$rowCategory['Category']."'>".$rowCategory['Category']."</option>";
}
//mysqli_close($connectionView)
?>
</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" id="order_by" name="order_by">
<option selected value="ORDER BY CurrentPrice ASC">Price (low to high)</option>
<option value="ORDER BY CurrentPrice DESC">Price (high to low)</option>
<option value="ORDER BY (a.EndingTime - CURRENT_TIMESTAMP) ASC">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
// Retrieve these from the URL
if (!isset($_GET['keyword'])) {
// TODO: Define behavior if a keyword has not been specified.
$keyword = "%";
}
else {
$keyword = $_GET['keyword'];
}
if (!isset($_GET['cat']) or $_GET['cat'] == "a.Category") {
// TODO: Define behavior if a category has not been specified.
$category = "a.Category";
}
else {
$category = "'".$_GET['cat']."'";
}
if (!isset($_GET['order_by'])) {
// TODO: Define behavior if an order_by value has not been specified.
$ordering = "";
}
else {
$ordering = $_GET['order_by'];
}
if (!isset($_GET['page'])) {
$curr_page = 1;
}
else {
$curr_page = $_GET['page'];
}
//echo $ordering;
/* TODO: Use above values to construct a query. Use this query to
retrieve data from the database. (If there is no form data entered,
decide on appropriate default value/default query to make. */
$results_per_page = 10;
$querryItemList = <<<QUERRYTEXT
SELECT
a.AuctionID,
a.ItemName,
a.ItemDescription,
a.StartingPrice,
a.EndingTime,
COUNT(b.BidID) AS 'CountBids',
MAX(b.BidPrice) AS 'bidPrice',
IF(
MAX(bidPrice) IS NULL,
a.StartingPrice,
MAX(bidPrice)
) AS 'CurrentPrice',
a.StartingPrice
FROM
auctions a
LEFT JOIN bids b ON a.AuctionID = b.AuctionID
WHERE
a.ItemName LIKE '%{$keyword}%'
AND (a.EndingTime - CURRENT_TIMESTAMP) > 0
AND a.Category = {$category}
GROUP BY
a.AuctionID,
a.ItemName,
a.ItemDescription,
a.StartingPrice,
a.EndingTime {$ordering}
QUERRYTEXT;
//echo $querryItemList;
$limiter = " LIMIT ".strval(($curr_page-1)*$results_per_page).", ".strval($results_per_page);
$querryWithLimitItemPerPage = $querryItemList.$limiter;
$resultforCounting = mysqli_query($connectionView, $querryItemList);
if ($resultforCounting)
{
// it return number of rows in the table.
$num_results = mysqli_num_rows($resultforCounting);
}
//$num_results = mysqli_num_rows($resultforCounting); // TODO: Calculate me for real
//echo $num_results;
$max_page = ceil($num_results / $results_per_page);
//echo($max_page);
//echo($num_results);
//mysqli_close($connectionView); //Don't close it here
?>
<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
// Demonstration of what listings will look like using dummy data.
$search_query_result = mysqli_query($connectionView, $querryWithLimitItemPerPage);
if ($search_query_result->num_rows > 0) {
while ($row = mysqli_fetch_array($search_query_result)){
$item_id = $row['AuctionID'];
$title = $row['ItemName'];
$description = $row['ItemDescription'];
$current_price = $row['CurrentPrice'];
$num_bids = $row['CountBids'];
$end_date = new DateTime($row['EndingTime']);
// This uses a function defined in utilities.php
print_listing_li($item_id, $title, $description, $current_price, $num_bids, $end_date);
}
}
else{
echo 'No result matches the search criteria.';
}
?>
</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>');
}
?>
</ul>
</nav>
</div>
<?php include_once("footer.php")?>