-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforum-post.php
266 lines (224 loc) · 12.1 KB
/
forum-post.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
<?php $title = "Post" ?>
<?php include "includes/components/header.php" ?>
<?php include "includes/components/navbar.php" ?>
<?php
$post = getForumPost($_GET["id"]);
$author = getUserByID($post["poster"]);
?>
<!-- Page content-->
<div class="container mt-5">
<div class="row">
<div class="col-lg-8">
<!-- Post content-->
<article>
<!-- Post header-->
<header class="mb-4">
<!-- Post title-->
<h1 class="fw-bolder mb-1"><?= $post["title"] ?></h1>
<!-- Post meta content-->
<div class="text-muted fst-italic mb-2">Posted on <?= date('F d, Y', strtotime($post["date"])); ?> by <strong><?= $author["first_name"] . " " . $author["last_name"] ?></strong></div>
<!-- Post categories-->
<?php
$tags = explode(",", $post["tags"]);
foreach ($tags as $key => $value) {
echo "<p class='badge bg-secondary text-decoration-none link-light'>$value</p> ";
}
?>
</header>
<!-- Post content-->
<section class="mb-5">
<hr>
<p class="fs-5 mb-4"><?= $post["content"] ?></p>
</section>
<?php
$p = $_GET["id"];
if (isset($_SESSION["id"])) {
$user = $_SESSION["id"];
}
$sql = "SELECT * FROM forum_upvotes WHERE post='$p' AND upvoted_by='$user'";
$query = mysqli_query($connection, $sql);
if (mysqli_num_rows($query) < 1) {
echo "
<div style='text-align:right;'>
<p>
<strong>{$post['upvotes']} Upvotes: </strong>
<strong><a style='text-decoration: none; ' href='forum-post.php?id={$post['id']}&action=upvote'>Upvote ❤️</a></strong>
</p>
</div>
";
} else {
echo "
<div style='text-align:right;'>
<p>
<strong>{$post['upvotes']} Upvotes: </strong>
<strong>Upvoted ❤️</strong>
</p>
</div>
";
}
?>
<br>
<br>
</article>
<!-- Comments section-->
<section class="mb-5">
<div class="card bg-light">
<div class="card-body">
<!-- Comment form-->
<?php
global $connection;
if (isset($_GET["action"])) {
$post = $_GET["id"];
$user = $_SESSION["id"];
$sql = "SELECT * FROM forum_upvotes WHERE post=$post AND upvoted_by='$user'";
$query = mysqli_query($connection, $sql);
if (mysqli_num_rows($query) < 1) {
}
$sql = "INSERT INTO forum_upvotes(`post`, `upvoted_by`) VALUES(?,?)";
$query = $connection->prepare($sql);
$query->bind_param("ii", $post, $user);
$query->execute();
$sql = "UPDATE forums SET upvotes=(SELECT COUNT(*) FROM forum_upvotes WHERE post='$post') WHERE id='$post'";
$query = mysqli_query($connection, $sql);
header("Location: forum-post.php?id=" . $_GET["id"]);
}
if (isset($_SESSION["id"])) {
echo "
<form action='forum-post.php?id={$post['id']}' method='POST' class='mb-4'>
<textarea name='content' class='form-control' rows='3' placeholder='Leave a comment!'></textarea>
<br>
<input id='btn' name='comment' type='submit' class='btn btn-primary' value='Post Comment'>
</form>
";
} else {
echo "
<a href='login.php'>Login</a> to post a comment!<br>
<br>
";
}
?>
<hr>
<br>
<div class="comment-area">
<?php
global $connection;
if (isset($_POST["comment"])) {
$commenter = $_SESSION["id"];
$commented_to = $_GET["id"];
$content = $_POST["content"];
$date = date("Y-m-d");
$sql = "INSERT INTO forum_comments(`commenter`, `commented_to`, `content`, `date`) VALUES(?,?,?,?)";
$query = $connection->prepare($sql);
$query->bind_param("iiss", $commenter, $commented_to, $content, $date);
$query->execute();
$sql = "UPDATE forums SET comments=(SELECT COUNT(*) FROM forum_comments WHERE commented_to='$commented_to') WHERE id='$commented_to'";
$query = mysqli_query($connection, $sql) or die("Failed " . mysqli_error($connection));
header("Location: forum-post.php?id=$commented_to");
}
$comments = getAllCommentsByForumPostID($_GET["id"]);
while ($row = mysqli_fetch_assoc($comments)) {
$user = getUserByID($row['commenter']);
if ($user["role"] == 2) {
echo "
<div class='d-flex'>
<div class='flex-shrink-0'><img width='50px' class='rounded-circle' src='images/avatars/{$user['avatar']}' alt='...' /></div>
<div class='ms-3'>
<div class='fw-bold'>{$user['username']} - <i>Counselor</i></div>
{$row['content']}
</div>
</div>
<hr>
";
} else {
echo "
<div class='d-flex'>
<div class='flex-shrink-0'><img width='50px' class='rounded-circle' src='images/avatars/{$user['avatar']}' alt='...' /></div>
<div class='ms-3'>
<div class='fw-bold'>{$user['username']}</div>
{$row['content']}
<div>
<a class='reply' style='color: blue; cursor: pointer;'>Reply</a>
<form class='reply-form' action='forum-post.php?id={$post['id']}' method='POST' style='display: none;'>
<textarea name='content' class='form-control' rows='3' placeholder='Leave a reply!'></textarea>
<input name='comment' type='submit' class='btn btn-primary' value='Post Comment'>
</form>
</div>
</div>
</div>
<hr>
";
}
}
?>
</div>
<!-- Comment with nested comments-->
<!-- <div class="d-flex mb-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="https://dummyimage.com/50x50/ced4da/6c757d.jpg" alt="..." /></div>
<div class="ms-3">
<div class="fw-bold">Commenter Name</div>
If you're going to lead a space frontier, it has to be government; it'll never be private enterprise. Because the space frontier is dangerous, and it's expensive, and it has unquantified risks.
<div>
<a class="reply" style="color: blue; cursor: pointer;">Reply</a>
<form id='reply-form' action="post.php?blog=" <?= $blog["id"] ?> method="POST" style="display: none;">
<textarea name='content' class='form-control' rows='3' placeholder='Leave a reply!'></textarea>
<input id='btn' name='comment' type='submit' class='btn btn-primary' value='Post Comment'>
</form>
</div>
<div class="d-flex mt-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="https://dummyimage.com/50x50/ced4da/6c757d.jpg" alt="..." /></div>
<div class="ms-3">
<div class="fw-bold">Commenter Name</div>
And under those conditions, you cannot establish a capital-market evaluation of that enterprise. You can't get investors.
</div>
</div>
</div>
</div> -->
</div>
</div>
</section>
</div>
<!-- Side widgets -->
<div class="col-lg-4">
<!-- Categories widget-->
<div class="card mb-4">
<div class="card-header">Posts Like This</div>
<div class="card-body">
<div class="row">
<?php
$others = getForumPostsLikeThese($post["tags"], $post['id'])
?>
<div class="col-sm-6">
<ul class="list-unstyled mb-0">
<?php
$i = 1;
while ($row = mysqli_fetch_assoc($others)) {
if ($i === 3) {
break;
}
echo "<li><a href='forum-post.php?id={$row['id']}'>{$row['title']}</a></li>";
}
?>
</ul>
</div>
<div class="col-sm-6">
<ul class="list-unstyled mb-0">
<?php
$i = 1;
while ($row = mysqli_fetch_assoc($others)) {
if ($i === 3) {
break;
}
echo "<li><a href='forum-post.php?id={$row['id']}'>{$row['title']}</a></li>";
}
?>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Footer-->
<script src="js/reply.js"></script>
<?php include "includes/components/footer.php" ?>