-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewpost.php
More file actions
257 lines (210 loc) · 8.45 KB
/
viewpost.php
File metadata and controls
257 lines (210 loc) · 8.45 KB
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
<?php
require ('includes/config.php');
//get post by ID query from database.
$sql = 'SELECT * FROM `blog_posts` WHERE `postID` = :postID ';
$result = $db->prepare($sql);
$result->bindParam(':postID', $_GET['id']);
$result->execute();
//save post contents in $thisPost.
$thisPost = $result->fetch();
//if post does not exists redirect user.
if ($thisPost['postID'] == '')
{
header('Location: error404.html');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- meta tags -->
<meta name="author" content="Sepand JamshidPour">
<meta name="description" content="A Simple Blog with PHP and Bootstrap">
<meta name="keywords" content="html, css, php, Bootstrap">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="files/stylesheets/bootstrap/bootstrap.min.css">
<title>Blog - <?php echo $thisPost['postTitle'];?></title>
</head>
<body class="bg-dark">
<div class="container">
<div class="pt-5">
<ul class="breadcrumb">
<li class="breadcrumb-item">My Blog</li>
<li class="breadcrumb-item"><a href="index.php">Home</a></li>
<li class="breadcrumb-item"><a href="#"><?=$thisPost['postTitle']?></a></li>
</ul>
</div>
<!-- main content -->
<!-- show post-->
<main class="container bg-light py-3 my-5">
<?php
echo '<div>';
echo '<h1>'.$thisPost['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y', strtotime($thisPost['postDate'])).'</p>';
echo '<p>'.$thisPost['postCont'].'</p>';
echo '</div>';
?>
</main>
<!-- end post -->
<!-- comments section -->
<!-- add comment -->
<!-- php code for validation and process new comment -->
<?php
//if form has been submitted process it
if (isset($_POST['submit']))
{
function strMapFunction($v)
{
return strtok($v, '&');
}
$_POST = array_map('strMapFunction', $_POST);
//collect form data
extract($_POST);
//very basic validation
if ($commentName == '')
{
$error[] = 'Please enter the name.';
}
if ($commentEmail == '')
{
$error[] = 'Please enter the email.';
}
if ($commentText == '')
{
$error[] = 'Please enter the comment.';
}
/*
if no error has been set then
insert the data into the database
*/
if (!isset($error))
{
try {
$commentDate = date('Y-m-d H:i:s');
$commentStatus = false;
$postID = $_GET['id'];
$sql = 'INSERT INTO blog_comments (commentName, commentEmail, commentText, commentDate, commentStatus, postID)
value (:commentName, :commentEmail, :commentText, :commentDate, :commentStatus, :postID)';
$result = $db->prepare($sql);
$result->bindParam(':commentName', $commentName);
$result->bindParam(':commentEmail', $commentEmail);
$result->bindParam(':commentText', $commentText);
$result->bindParam(':commentDate', $commentDate);
$result->bindParam(':commentStatus', $commentStatus);
$result->bindParam(':postID', $postID);
$result->execute();
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<!-- add comment form -->
<div class="bg-light rounded">
<h4 class="font-italic font-weight-bold card-header border-bottom-0">Share your thoughts about this post:</h4>
<?php
/*
* check for any errors
* if has been any errors
* display them
* */
if (isset($_POST['submit']))
{
if (isset($error)) {
echo '<div class="container-fluid m-4">';
foreach ($error as $err) {
echo '<div class="alert alert-danger alert-dismissible fade show col-sm-7 col-md-5 col-lg-4" role="alert">';
echo ' <span>' . $err . '</span>';
echo ' <button type="button" class="close" data-dismiss="alert" aria-label="Close">';
echo ' <span aria-hidden="true">×</span>';
echo ' </button>';
echo '</div>';
}
echo '</div>';
} else {
$message = "Comment Submitted Successfully.";
echo '<div class="container-fluid m-4">';
echo '<div class="alert alert-success alert-dismissible fade show col-sm-7 col-md-5 col-lg-4" role="alert">';
echo ' <span>' . $message . '</span>';
echo ' <button type="button" class="close" data-dismiss="alert" aria-label="Close">';
echo ' <span aria-hidden="true">×</span>';
echo ' </button>';
echo '</div>';
echo '</div>';
}
}
?>
<form method="post" class="card-body">
<div class="row ml-1">
<div class="col-md-6 col-lg-4">
<div class="form-group">
<label for="commentName">Name</label>
<input class="form-control" type="text" name="commentName" id="commentName" value="<?php if (isset($error)){ echo $_POST['commentName'];}?>">
</div>
</div>
<div class="col-md-6 col-lg-4">
<div class="form-group">
<label for="commentEmail">Email</label>
<input class="form-control" type="email" name="commentEmail" id="commentEmail" value="<?php if (isset($error)){ echo $_POST['commentEmail'];}?>" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
</div>
</div>
<div class="col-md-12 col-lg-8">
<div class="form-group">
<label for="commentText">Comment</label>
<textarea class="form-control" name="commentText" id="commentText" rows="5"><?php if (isset($error)){ echo $_POST['commentText'];}?></textarea>
</div>
</div>
<div class="form-group ml-3">
<input type="submit" name="submit" value="Add Comment" class="btn btn-outline-success">
</div>
</form>
</div>
<!-- show comments -->
<section class="bg-light my-5 rounded">
<h3 class="font-italic font-weight-bold card-header border-bottom-0">Comments</h3>
<div class="card-body">
<?php
try {
//show all comments of this post
$postID = $_GET['id'];
$commentStatus = true;
$sql = 'SELECT commentName, commentText, commentDate FROM blog_comments WHERE postID = :postID AND commentStatus = :commentStatus ORDER BY commentDate DESC';
$result = $db->prepare($sql);
$result->bindParam(':postID', $postID);
$result->bindParam(':commentStatus', $commentStatus);
$result->execute();
while ($comments = $result->fetch())
{
echo '<div class="jumbotron p-3 pl-5">';
echo ' <span class="d-block font-weight-bold">'.$comments['commentName'].'</span>';
echo ' <small class="d-block font-weight-light">'.date('jS F Y H:i:s', strtotime($comments['commentDate'])).'</small>';
echo ' <p class="m-2">'.$comments['commentText'].'</p>';
echo '</div>';
}
}
catch (PDOException $e)
{
$e->getMessage();
}
?>
</div>
</section>
</div>
<!-- end content -->
<!--footer section-->
<footer class="py-5 bg-secondary">
<div class="container text-center text-white">
<p>This weblog is for practice the PHP</p>
<p>Sepand JamshidPour | 2020</p>
</div>
</footer>
<!--add Bootstrap and jQuery JavaScript files-->
<script type="text/javascript" rel="script" src="files/scripts/bootstrap/jquery-3.5.1.min.js"></script>
<script type="text/javascript" rel="script" src="files/scripts/bootstrap/bootstrap.min.js"></script>
</body>
</html>