-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.php
329 lines (287 loc) · 12.2 KB
/
posts.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
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
<?php
require "backend/config/config.php";
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($mysqli->connect_errno)
{
echo $mysqli->connect_error;
exit();
}
$category = $_GET['category'];
if(!isset($_GET['sort_by']) || empty($_GET['sort_by'])) {
$sort_by = "";
}
$sql_prepared = "SELECT * FROM post WHERE category = ?;";
$statement = $mysqli->prepare($sql_prepared);
$statement->bind_param("s",$category);
$execute_item = $statement->execute();
if(!$execute_item)
{
echo $mysqli->error;
exit();
}
$result_item = $statement->get_result();
$result_arr = array();
while($row = $result_item->fetch_assoc())
{
$user_id = $row['author_id'];
$sql_user = "SELECT * FROM user WHERE user_id = " . $user_id . ";";
$result_user = $mysqli->query($sql_user);
if(!$result_user)
{
echo $mysqli->error;
exit();
}
while($row_user = $result_user->fetch_assoc())
{
$row['username'] = $row_user['username'];
$row['contribution'] = $row_user['contribution'];
// email for now, change to wechat_id later
}
array_push($result_arr,$row);
}
// Sorts result array
if ($sort_by == "time") {
usort($result_arr, function($a, $b) {
// Custom closure to sort by DateTime
$ad = new DateTime($a['publish_time']);
$bd = new DateTime($b['publish_time']);
if ($ad == $bd) {
return 0;
}
return $ad > $bd ? -1 : 1;
});
} else if ($sort_by == "contribution") {
usort($result_arr, function($a, $b) {
// Custom closure to sort by contribution
$ac = $a['contribution'];
$bc = $b['contribution'];
if ($ac == $bc) {
return 0;
}
return $ac > $bc ? -1 : 1;
});
}
$statement->close();
$mysqli->close();
$cnt = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>posts page</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav class="_nav">
<div class="_nav__box--left">
<h4><a href = "index.php" style = "text-decoration:none; color:black;">coronaHub</a></h4>
</div>
<div class="_nav__box--right">
<?php if(isset($_SESSION['username']) && !empty($_SESSION['username'])) : ?>
<div class="_nav__user-box">
<img src="/assets/user-photo.jpg" alt="user photo">
<h6><span><a href = "profile.php"><?php echo $_SESSION['username']; ?> </a></span></h6>
</div>
<div class="_nav__upload-box">
<a href="item_upload.php" class="btn btn-lg btn-info mr-3">我要上传</a>
</div>
<div class="_nav__upload-box">
<a href="backend/user/logout.php" class="btn btn-lg btn-info">退出</a>
</div>
<?php else: ?>
<div class="_nav__login-box">
<div><a href="login.php">登录</a> /
<a href="signup.php">注册</a></div>
</div>
<div class="_nav__upload-box">
<a href="index.php?error=请先登录" class="btn btn-lg btn-info">我要上传</a>
</div>
<?php endif; ?>
</div>
</nav>
<nav class="nav-secondary">
<div class="header-box">
<h6><a href="items.php?category=mask">口罩</a></h6>
<img src="assets/nav-mask.png" alt="mask img">
</div>
<div class="header-box">
<h6><a href="items.php?category=goggle">护目镜</a></h6>
<img src="assets/nav-glass.png" alt="glass img">
</div>
<div class="header-box">
<h6><a href="items.php?category=sanitizer">消毒用具</a></h6>
<img src="assets/nav-discontaminate.png" alt="discontaminate img">
</div>
<div class="header-box">
<h6><a href="items.php?category=necessity">日常用品</a></h6>
<img src="assets/nav-necessity.png" alt="necessity img">
</div>
<div class="header-box">
<h6><a href = "posts.php?category=back">回国</a></h6>
<img src="assets/nav-back.png" alt="back img">
</div>
<div class="header-box">
<h6><a href = "posts.php?category=stay">留守</a></h6>
<img src="assets/nav-stay.png" alt="stay img">
</div>
</nav>
<form class="container-fluid" id="posts-container" action="/posts.php" method="GET">
<div class="header-nav">
<h3 class="title-box">
<?php
if($category=="back")
echo "回国";
else
echo "留守";
?>
</h3>
<div class="search-box">
<label for="search-input">搜索:</label>
<input type="text" class="search-input" id="search-input" placeholder="关键词">
</div>
<div class="sorting-box input-group">
<select name="sort_by" class="custom-select">
<option <?php if ($sort_by == "") echo 'selected';?> disabled>排序方式</option>
<option <?php if ($sort_by == "time") echo 'selected';?> value="time">时间</option>
<option <?php if ($sort_by == "contribution") echo 'selected';?> value="contribution">贡献值</option>
</select>
</div>
<input type="hidden" name="category" value="<?php echo $category; ?>" />
<button class="btn btn-lg btn-primary confirm-button" type="submit" >确认</button>
<div class="pagination-box">
<ul class="pagination">
<li class="page-item disabled"><a class="page-link" href="#">前一页</a></li>
<li class="page-item active"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">后一页</a></li>
</ul>
</div>
</div>
<div class="posts-table">
<div class="posts-table__header">
<div class="post-button-container">
<a href="post_add.php?category=<?php echo $category;?>" class="btn btn-lg btn-outline-dark">发帖</a>
</div>
<div class="post-info-container">
<h6>发布时间</h6>
<h6>作者</h6>
<h6>阅览</h6>
<h6>贡献值</h6>
</div>
</div>
<div class="posts-table__content">
<div class="posts-table__row top">
<div class="title-container">
<a href="#" class="title">1. 震惊!洛杉矶大华惊现哥斯拉</a>
<span class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea com ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea com
</span>
</div>
<div class="post-info-container">
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-calendar"></use>
</svg>
2020/2/13
</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-profile"></use>
</svg>
同同同</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-eye"></use>
</svg>
17</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-thumbs-up"></use>
</svg>
201</span>
</div>
</div>
<div class="posts-table__row top">
<div class="title-container">
<a href="#" class="title">1. 震惊!洛杉矶大华惊现哥斯拉</a>
<span class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea com ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea com
</span>
</div>
<div class="post-info-container">
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-calendar"></use>
</svg>
2020/2/13
</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-profile"></use>
</svg>
同同同</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-eye"></use>
</svg>
17</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-thumbs-up"></use>
</svg>
201</span>
</div>
</div>
<!-- Below are user posts -->
<?php foreach($result_arr as $row) { ?>
<div class="posts-table__row">
<div class="title-container">
<a href="post_detail.php?id=<?php echo $row['post_id']; ?>" class="title">
<?php
echo $cnt;
echo ".";
$cnt = $cnt+1;
echo $row['headline'];
?>
</a>
<span class="content">
<?php echo $row['content']; ?>
</span>
</div>
<div class="post-info-container">
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-calendar"></use>
</svg>
<?php echo $row['publish_time']; ?>
</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-profile"></use>
</svg>
<?php echo $row['username']; ?>
</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-eye"></use>
</svg>
<?php echo $row['view']; ?>
</span>
<span>
<svg>
<use xlink:href="/assets/sprite.svg#icon-thumbs-up"></use>
</svg>
<?php echo $row['contribution']; ?>
</span>
</div>
</div>
<?php }?>
</div>
</div>
</form>
<script src="./bundle.js"></script>
</body>
</html>