This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
357 lines (267 loc) · 12.8 KB
/
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
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
<?php include("includes/header.php"); ?>
<!-- Navigation -->
<?php include("includes/nav.php"); ?>
<?php
// ---------------------------------//
// Executing post request for like //
// ---------------------------------//
if(isset($_POST['liked'])){
$post_id = $_POST['post_id'];
$user_id = $_POST['user_id'];
// 1. FETCHING THE POST FROM THE DB AND THE POST'S DATA
$query = "SELECT * FROM posts WHERE post_id = {$post_id}";
$postResult = mysqli_query($connection, $query);
$post = mysqli_fetch_array($postResult);
$likes = $post['likes'];
if(mysqli_num_rows($postResult) >= 1)
{
echo $post['post_id'];
}
// 2. UPDATE INCREMENTING WITH LIKES
mysqli_query($connection, "UPDATE posts SET likes=$likes+1 WHERE post_id={$post_id}");
// 3. PUT DATA INTO LIKES
mysqli_query($connection, "INSERT INTO likes(user_id, post_id) VALUES({$user_id}, {$post_id})");
exit();
}
// -----------------------------------//
// Executing post request for unlike //
// -----------------------------------//
if(isset($_POST['unliked'])){
$post_id = $_POST['post_id'];
$user_id = $_POST['user_id'];
// 1. FETCHING THE POST
$query = "SELECT * FROM posts WHERE post_id = {$post_id}";
$postResult = mysqli_query($connection, $query);
$post = mysqli_fetch_array($postResult);
$likes = $post['likes'];
// 2. DELETE LIKES
mysqli_query($connection, "DELETE FROM likes WHERE post_id={$post_id} AND user_id={$user_id}");
// 3. UPDATE DECREMENTING WITH LIKE
mysqli_query($connection, "UPDATE posts SET likes=$likes-1 WHERE post_id={$post_id}");
exit();
}
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-8">
<?php
if(isset($_GET['p_id'])){
$the_post_id = $_GET['p_id'];
$check = query("SELECT * FROM posts WHERE post_id = {$the_post_id} AND user_id =".loggedInUserId()."");
$fetch = count_records($check);
$row = fetchRecords($check);
$status = $row['post_status'];
if($fetch == 0)
{
redirect("index");
}
$update_statement = mysqli_prepare($connection, "UPDATE posts SET post_view_count = post_view_count + 1 WHERE post_id = ?");
mysqli_stmt_bind_param($update_statement, "i", $the_post_id);
mysqli_stmt_execute($update_statement);
confirmQuery($update_statement);
if (isLoggedIn()) {
$stmt1 = mysqli_prepare($connection, "SELECT post_title, post_user, post_date, post_image, post_content FROM posts WHERE post_id = ? AND post_user = ?");
}
else {
redirect("index");
// $stmt2 = mysqli_prepare($connection , "SELECT post_title, post_user, post_date, post_image, post_content FROM posts WHERE post_id = ? AND post_status = ? ");
// $published = 'Published';
}
if(isset($stmt1)){
mysqli_stmt_bind_param($stmt1, "is", $the_post_id, $_SESSION['username']);
mysqli_stmt_execute($stmt1);
mysqli_stmt_bind_result($stmt1, $post_title, $post_user, $post_date, $post_image, $post_content);
$stmt = $stmt1;
}
// else {
// mysqli_stmt_bind_param($stmt2, "is", $the_post_id, $published);
// mysqli_stmt_execute($stmt2);
// mysqli_stmt_bind_result($stmt2, $post_title, $post_author, $post_date, $post_image, $post_content);
// $stmt = $stmt2;
// }
while(mysqli_stmt_fetch($stmt)) {
?>
<h1 class="page-header">
<span class="glyphicon glyphicon-bookmark"></span> Post
<?php
if($fetch != 0)
{
echo "<div class='pull-right '><small class='text text-warning'> ". $status."</small></div>";
}
?>
</h1>
<!-- First Blog Post -->
<h2>
<a href="#"><?php echo $post_title ?></a>
</h2>
<p class="lead">
by <a href="/project/cms/index"><?php echo ucfirst($post_user); ?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted <?php echo $post_date; ?></p>
<hr>
<img class="img-responsive" src="/project/cms/images/<?php echo imagePlaceholder($post_image); ?>" alt=""> <!-- it get data (img link) from db, it has been entered manually by me -->
<hr>
<p><?php echo $post_content ?></p>
<hr>
<?php
// FREEING RESULT
mysqli_stmt_free_result($stmt);
?>
<?php
if(isLoggedIn()){
?>
<div class="row">
<p class="pull-right">
<a class="<?php echo userLikedThisPost($the_post_id) ? 'unlike' : 'like'; ?>" href="">
<span class="glyphicon glyphicon-thumbs-up" data-toggle="tooltip" data-placement="top" title="<?php echo userLikedThisPost($the_post_id) ? ' I liked this before' : ' Want to like it?'; ?>"></span>
<?php echo userLikedThisPost($the_post_id) ? ' Unlike' : ' Like'; ?>
</a>
</p>
</div>
<?php } else { ?>
<div class="row">
<p class="pull-right">You need to <a href="/project/cms/login">login</a> to like this post.</p>
</div>
<?php } ?>
<div class="row">
<small class="pull-right"><i class="glyphicon glyphicon-user"></i> <?php getPostLikes($the_post_id); ?> people like this.</small>
</div>
<br>
<div class="clearfix">
</div>
<!-- While loop end -->
<?php } ?>
<!-- Blog Comments -->
<?php
$message = '';
if(isset($_POST['create_comment']))
{
$the_post_id = $_GET['p_id']; //using the post id in the url
$comment_author = $_POST['comment_author'];
$comment_email = $_POST['comment_email'];
$comment_content = $_POST['comment_content'];
if(!empty($comment_author) && !empty($comment_email) && !empty($comment_content)) {
$query = "INSERT INTO comments (comment_post_id, comment_author, comment_email, comment_content, comment_status, comment_date)";
$query .= " VALUES ( $the_post_id, '{$comment_author}', '{$comment_email}', '{$comment_content}', 'Unapproved', now() )";
$create_comment_query = mysqli_query($connection, $query);
confirmQuery($create_comment_query);
//query for comment count
$query = "UPDATE posts SET post_comment_count = post_comment_count + 1";
$query .= " WHERE post_id = $the_post_id";
$update_comment_count = mysqli_query($connection, $query);
$message = "<div class='alert alert-success'>";
$message .= "Your comment has been successfully submitted ";
$message .= "</div>";
}
else
{
$message = "<div class='alert alert-danger'>";
$message .= "Field can't be empty. Please try again. ";
$message .= "</div>";
}
}
?>
<!-- Comments Form -->
<div class="well">
<h3><span class="glyphicon glyphicon-envelope"></span> Leave a Comment:</h3>
<?php echo $message; ?>
<form action="" method="post" role="form">
<div class="form-group">
<label for="comment_author">Name</label>
<input type="text" class="form-control" name="comment_author" placeholder="Please enter your name">
</div>
<div class="form-group">
<label for="comment_author">Email</label>
<input type="email" class="form-control" name="comment_email" placeholder="Please enter your email">
</div>
<div class="form-group">
<label for="comment_content">Your Comment</label>
<textarea name="comment_content" class="form-control" rows="3" placeholder="Please enter your comment"></textarea>
</div>
<button type="submit" name="create_comment" class="btn btn-primary" title="Submit your comment"><span class="glyphicon glyphicon-pencil"></span> Submit</button>
</form>
</div>
<br>
<!-- Posted Comments -->
<h1><span class="glyphicon glyphicon-comment"></span> Comments</h1>
<hr>
<?php
$query = "SELECT * FROM comments WHERE comment_post_id = {$the_post_id}";
$query .= " AND comment_status = 'Approved'";
$query .= " ORDER BY comment_id DESC ";
$select_comment_query = mysqli_query($connection, $query);
confirmQuery($select_comment_query);
if(mysqli_num_rows($select_comment_query) <1 )
{
echo "<div class='alert alert-info'>";
echo " Comments is not yet available. ";
echo "</div>";
}
while ($row = mysqli_fetch_array($select_comment_query)) {
$comment_date = $row['comment_date'];
$comment_content = $row['comment_content'];
$comment_author = $row['comment_author'];
?>
<!-- Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $comment_author ?>
<small><?php echo $comment_date ?></small>
</h4>
<?php echo $comment_content ?>
</div>
</div>
<?php }
} else {
redirect("index");
}
?>
</div>
<!-- Blog Sidebar Widgets Column -->
<?php include("includes/sidebar.php"); ?>
</div>
<!-- /.row -->
</div>
<br>
<hr>
<?php include("includes/footer.php"); ?>
<script>
// -------------------------//
// post function with Ajax //
// -------------------------//
$(document).ready(function(){
// Tooltip function
$("[data-toggle='tooltip']").tooltip();
var post_id = <?php echo $the_post_id; ?>;
var user_id = <?php echo loggedInUserId(); ?>;
// LIKE POST AJAX
$('.like').click(function(){
$.ajax({
url : "/project/cms/post.php?p_id=<?php echo $the_post_id; ?>",
type: 'post',
data: {
'liked': 1,
'post_id': post_id,
'user_id': user_id
}
});
});
// UNLIKE POST AJAX
$('.unlike').click(function(){
$.ajax({
url : "/project/cms/post.php?p_id=<?php echo $the_post_id; ?>",
type: 'post',
data: {
'unliked': 1,
'post_id': post_id,
'user_id': user_id
}
});
});
});
</script>