-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-post.php
139 lines (105 loc) · 5.06 KB
/
single-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
<?php
require_once('./database/query-build.php');
$getData1 = new QueryBuild('localhost', 'blog', 'arsen', 'arsenroot');
$getData1->connect();
$getData2 = new QueryBuild('localhost', 'blog', 'arsen', 'arsenroot');
$getData2->connect();
$post_id = $_GET['post_id'];
$getData2->setQuery("SELECT posts.id AS id, posts.title AS title, posts.body AS body,
posts.created_at AS created_at, users.firstName AS firstName, users.lastName as lastName
FROM posts
LEFT JOIN users
ON posts.author = users.id WHERE posts.id = :post_id");
$getData1->setQuery('SELECT * FROM comments WHERE post_id = :post_id');
$post = $getData2->fetchSingle($post_id);
$comments = $getData1->fetchMulti($post_id);
?>
<!doctype html>
<html lang="en">
<head>
<?php include('./templates/head.php'); ?>
</head>
<body>
<?php include('./templates/header.php'); ?>
<main role="main" class="container">
<div class="row">
<div class="col-sm-8 blog-main">
<div class="blog-post">
<a href="./single-post.php?post_id=<?php echo $post['id']; ?>">
<h2 class="blog-post-title"><?php echo $post['title'] ?></h2>
</a>
<p class="blog-post-meta"><?php echo $post['created_at'] . " "; ?> by <a href="#"><?php echo " " . $post['firstName'] . " " . $post['lastName']; ?></a></p>
<button type="button" id="del-post-btn" class="btn btn-primary">Delete Post</button>
<hr>
<p><?php echo $post['body']; ?></p>
</div>
<div class="alert alert-danger" id="alertBox">
<strong>Failed submit!</strong> Please fill out all remaining fields.
</div>
<h2>Add New Comment</h2>
<div class="add-new-comment">
<form action="./create-comment.php" method="POST" class="add-comm-form">
<input type="hidden" id="postId" name="post_id" value="<?php echo $post['id']; ?>">
<div>
<h4>Full Name:</h4> <input type="text" name="fullname" id="fullName" placeholder="John Doe" required>
</div>
<div>
<h4>Your Comment:</h4>
</div>
<div>
<textarea name="text" id="commentBox" required></textarea>
</div>
<button type=" submit" id="commentSubmit" class="btn btn-default">Submit</button>
</form>
</div>
<button type=" button" class="btn btn-default" id="show-hide-btn">Hide Comments</button>
<div class="comments" id="comment-section">
<ul class="comment-list">
<?php foreach ($comments as $comm) { ?>
<li>
<strong><?php echo $comm['author'] . ': '; ?></strong><?php echo $comm['text']; ?>
<button type="button" class="btn btn-default"><a href="./delete-comment.php?comm_id=<?php echo $comm['id']; ?>&post_id=<?php echo $_GET['post_id']; ?>">Delete</a></button>
</li>
<hr>
<?php } ?>
</ul>
</div>
</div><!-- /.blog-main -->
<?php include('./templates/sidebar.php'); ?>
</div><!-- /.row -->
</main><!-- /.container -->
<?php include('./templates/footer.php') ?>
<script type="text/javascript">
const showHideBtn = document.getElementById("show-hide-btn");
const comments = document.getElementById("comment-section");
showHideBtn.addEventListener("click", function() {
if (comments.style.display === "none") {
comments.style.display = "block";
showHideBtn.innerHTML = "Hide Comments";
} else {
comments.style.display = "none";
showHideBtn.innerHTML = "Show Comments";
}
});
const fullName = document.getElementById('fullName').value;
const commentBox = document.getElementById('commentBox').value;
const alertBox = document.getElementById('alertBox');
const commentSubmitBtn = document.getElementById('commentSubmit');
commentSubmitBtn.addEventListener('click', function() {
if (fullName === "" || commentBox === "") {
alertBox.style.display = 'block';
}
});
const delPostBtn = document.getElementById('del-post-btn');
const postId = document.getElementById('postId').value;
delPostBtn.addEventListener('click', function() {
let del = confirm("Do you really want to delete this post?");
if (del === true) {
let delPost = "/delete-post.php?post_id=";
let delLoc = delPost.concat(postId);
window.location.href = delLoc;
}
});
</script>
</body>
</html>