-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.php
85 lines (75 loc) · 3.14 KB
/
home.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
<link rel="stylesheet" href="public/styles/reset.css">
<link rel="stylesheet" href="public/styles/styles.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
</head>
<body>
<?php include "header.php" ?>
<div class="container">
<?php
include 'utils/db_connect.php';
// Check if the user ID session variable is set
if (isset($_SESSION['id'])) {
$userId = $_SESSION['id']; // Get the user ID from the session
$sql = "SELECT * FROM blogposts WHERE authorid = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
// Check if there are any posts
if ($result->num_rows > 0) {
// Loop through each post and generate a card
while ($post = $result->fetch_assoc()) {
?>
<div class="card">
<div class="card-container">
<div class="card-header"><h1><?php echo $post['title']; ?></h1></div>
<div class="card-body">
<p><?php echo $post['content']; ?></p>
<p>Date: <span id="post-date-<?php echo $post['id']; ?>"><?php echo $post['date']; ?></span></p>
</div>
<div class="card-footer">
<p>
<a class="btn btn-delete" href="#" onclick="deletePost(<?php echo $post['id']; ?>)">DELETE</a>
<a class="btn btn-edit" href="blogposts/updatepostform.php?blogpost_id=<?php echo $post['id']; ?>">EDIT</a>
</p>
</div>
</div>
</div>
<script>
// Format and display the date using Moment.js
var postDate = moment("<?php echo $post['date']; ?>").format('Do [of] MMMM YYYY hh:mm:ss A');
document.getElementById("post-date-<?php echo $post['id']; ?>").textContent = postDate;
</script>
<?php
}
} else {
echo "<p class='info-message'>No posts found.</p>";
}
$stmt->close();
} else {
echo "User not logged in.";
}
$conn->close();
?>
<div class="card">
<a class="btn btn-create" href="blogposts/createpostform.php">CREATE NEW POST</a>
</div>
</div>
</body>
<script>
function deletePost(postId) {
var confirmDelete = confirm("Are you sure you want to delete this post?");
if (confirmDelete) {
// Redirect to the deletepost.php page passing the postId as a parameter
window.location.href = "blogposts/deletepost.php?id=" + postId;
}
}
</script>
</html>