/
post.php
177 lines (132 loc) · 5.83 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
<?php
if(!isset($_GET['id'])){
header('Location: index.php');
die();
}
if(!is_numeric($_GET['id'])){
header('Location: index.php');
die();
}
require('conf.php');
if(isset($_POST['commentsubbut'])){
$commentPostId = (int) $_POST['postId'];
$comment = strip_tags($_POST['comment']);
if(isset($_SESSION['uid'])){
$authorId = $_SESSION['uid'];
$authorUsername = $_SESSION['uname'];
$qry = '
INSERT INTO comments(post_id,author_id,author_username,content)
VALUES(:postid,:authorid,:username,:comment)
';
$insertComment = $conn->prepare($qry);
$insertComment->execute([
'postid' => $commentPostId,
'authorid' => $authorId,
'username' => $authorUsername,
'comment' => $comment
]);
}
header('Location: post.php?id='.$commentPostId);
die();
}
$postId = (int) $_GET['id'];
$qry = '
SELECT id, post_title, post_author, post_content, post_image, date_created
FROM posts
WHERE id = :id
';
$getPost = $conn->prepare($qry);
$getPost->execute([
'id' => $postId
]);
$post = $getPost->fetch(PDO::FETCH_OBJ);
$qry = '
SELECT * FROM comments
WHERE post_id = :postId
ORDER BY id DESC
';
$getPostComments = $conn->prepare($qry);
$getPostComments->execute([
'postId' => $postId
]);
$comments = $getPostComments->fetchAll(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html>
<head>
<?php require('partials/head.php'); ?>
<title>HB Blog | Home</title>
</head>
</body>
<!-- Header -->
<?php include('partials/header.php'); ?>
<main>
<div class="home-container">
<div class="home-container__one">
<div class="post-more">
<h1 class="post-more__title"><?= $post->post_title; ?></h1>
<p class="post-more__author">By <?= $post->post_author; ?></p>
<p class="post-more__created"><i class="fa fa-clock"></i> Posted at <?= date_format(date_create($post->date_created),'m-d-Y H:i') ?></p>
<div class="post-more__img-container">
<div class="post-more__img" style="background-image:url(./uploads/<?= $post->post_image; ?>);"></div>
</div>
<div class="post-more__content">
<?= $post->post_content; ?>
</div>
</div>
<?php if(!isset($_SESSION['uid'])): ?>
<div class="notif">
<p class="notif__msg">Please <a href="login.php">Sign In</a> or <a href="register.php">Register</a> to post a comment.</p>
</div>
<?php else: ?>
<div class="card">
<form class="comment-form" method="POST" action="post.php?id=<?= $post->id ?>">
<h2 class="heading-secondary">Submit A Comment</h2>
<textarea name="comment" id="comment" class="textarea-input comment-form__input" rows="8"></textarea>
<input type="hidden" name="postId" value="<?= $post->id ?>"/>
<button name="commentsubbut" id="commentsubbut" class="btn btn--primary">Submit</button>
</form>
</div>
<?php endif; ?>
<h2 class="heading-secondary">Comments</h2>
<?php if(!count($comments)): ?>
<p>There are no comments yet for this post. Be the first to comment!</p>
<?php else: ?>
<?php foreach($comments as $comment): ?>
<div class="post-comment">
<p class="post-comment__author"><?= $comment->author_username ?></p>
<p class="post-comment__created"><i class="fa fa-clock"></i> <?= date_format(date_create($comment->created_date), 'm/d/Y H:i') ?></p>
<p class="post-comment__content"><?= $comment->content ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="home-container__two">
<div class="card search-box">
<form>
<h2 class="heading-secondary"><i class="fa fa-search"></i> Search</h2>
<div class="fgrp">
<input type="text" class="text-input" placeholder="Search terms..."/>
</div>
<div class="fgrp">
<button class="btn btn--primary">Submit</button>
</div>
</form>
</div>
<div class="card most-recent-box">
<h2 class="heading-secondary">Most Recent</h2>
<p class="most-recent-box__no-recent">There are no recent posts to display.</p>
</div>
</div>
</div>
</main>
<footer class="footer">
<small style="font-size:1.2rem; display:inline-block; margin-top:1rem;">© 2019 HB Blog</small>
</footer>
<script type="application/javascript">
document.querySelector('.header__navtoggle').addEventListener('click',()=>{
document.querySelector('.header__two').classList.toggle('show');
});
</script>
</body>
</html>