30882-vm/post.php
Flatlogic Bot 8989de90b0 3
2025-09-17 10:37:57 +00:00

144 lines
6.1 KiB
PHP

<?php
require_once 'db/config.php';
$post_id = $_GET['id'] ?? null;
if (!$post_id || !filter_var($post_id, FILTER_VALIDATE_INT)) {
header("Location: index.php");
exit;
}
// Handle comment submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_comment') {
$name = trim($_POST['name'] ?? '');
$comment_text = trim($_POST['comment'] ?? '');
if (!empty($name) && !empty($comment_text)) {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO comments (post_id, name, comment) VALUES (?, ?, ?)");
$stmt->execute([$post_id, $name, $comment_text]);
header("Location: post.php?id=" . $post_id);
exit;
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
}
}
}
$post = null;
$comments = [];
try {
$pdo = db();
// Fetch post
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
$stmt->execute([$post_id]);
$post = $stmt->fetch();
// Fetch comments
$stmt = $pdo->prepare("SELECT * FROM comments WHERE post_id = ? ORDER BY created_at DESC");
$stmt->execute([$post_id]);
$comments = $stmt->fetchAll();
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
}
// If no post is found, redirect to the homepage
if (!$post) {
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($post['title']); ?> - My Awesome Blog</title>
<meta name="description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
<!-- Open Graph -->
<meta property="og:title" content="<?php echo htmlspecialchars($post['title']); ?>">
<meta property="og:description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
<meta property="og:type" content="article">
<meta property="og:url" content="http://<?php echo $_SERVER['HTTP_HOST']; ?>/post.php?id=<?php echo $post['id']; ?>">
<meta property="og:image" content="<?php echo htmlspecialchars($post['image_url']); ?>">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="/">My Awesome Blog</a>
</div>
</nav>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<article>
<header class="mb-4">
<h1 class="fw-bolder mb-1 display-5"><?php echo htmlspecialchars($post['title']); ?></h1>
<div class="text-muted fst-italic mb-2">Posted on <?php echo date('F j, Y', strtotime($post['created_at'])); ?></div>
</header>
<figure class="mb-4">
<img class="img-fluid rounded" src="<?php echo htmlspecialchars($post['image_url']); ?>" alt="Hero image for the blog post titled '<?php echo htmlspecialchars($post['title']); ?>'">
</figure>
<section class="mb-5 post-content fs-5">
<?php echo nl2br(htmlspecialchars($post['content'])); ?>
</section>
</article>
<section class="mb-5">
<div class="card bg-light">
<div class="card-body">
<!-- Comment form-->
<form class="mb-4" method="POST">
<input type="hidden" name="action" value="create_comment">
<div class="mb-3">
<label for="name" class="form-label">Your Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="comment" class="form-label">Your Comment</label>
<textarea class="form-control" rows="3" id="comment" name="comment" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit Comment</button>
</form>
<!-- Comments section-->
<?php foreach ($comments as $comment): ?>
<div class="d-flex mb-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="https://dummyimage.com/50x50/ced4da/6c757d.jpg" alt="..." /></div>
<div class="ms-3">
<div class="fw-bold"><?php echo htmlspecialchars($comment['name']); ?></div>
<?php echo htmlspecialchars($comment['comment']); ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<a href="/" class="btn btn-outline-primary">&larr; Back to all posts</a>
</div>
</div>
</main>
<footer class="footer mt-auto py-3 bg-white border-top">
<div class="container text-center">
<span class="text-muted">&copy; <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>