hjkhjkjhkhjkgjghjhgjhgjghjhghjghjhgjjnbvnvbnvk
bnmbnmbnhjkhjkhjkhjktyutyutyuytutyutyutyhgjgjghjgjghjguytutyry
/
home
/
wjcvelqp
/
biblog
/
Upload FileeE
HOME
<?php require_once 'config/database.php'; require_once 'includes/functions.php'; $post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($post_id > 0) { incrementViews($pdo, $post_id); $stmt = $pdo->prepare("SELECT p.*, c.name as category_name FROM posts p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?"); $stmt->execute([$post_id]); $post = $stmt->fetch(PDO::FETCH_ASSOC); } if (!$post) { die("Post not found!"); } // Handle Like if (isset($_POST['like'])) { likePost($pdo, $post_id); } // Handle Comment or Reply if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) { $name = trim($_POST['name']); $email = trim($_POST['email'] ?? ''); $comment_text = trim($_POST['comment_text']); $parent_id = isset($_POST['parent_id']) && $_POST['parent_id'] > 0 ? (int)$_POST['parent_id'] : null; if (!empty($name) && !empty($comment_text)) { if ($parent_id) { $stmt = $pdo->prepare("INSERT INTO comments (post_id, parent_id, name, email, comment) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$post_id, $parent_id, $name, $email, $comment_text]); } else { $stmt = $pdo->prepare("INSERT INTO comments (post_id, parent_id, name, email, comment) VALUES (?, NULL, ?, ?, ?)"); $stmt->execute([$post_id, $name, $email, $comment_text]); } header("Location: single.php?id=$post_id&success=1"); exit; } } $comment_success = isset($_GET['success']); // Fetch Comments $stmt = $pdo->prepare("SELECT * FROM comments WHERE post_id = ? AND status = 'approved' ORDER BY created_at ASC"); $stmt->execute([$post_id]); $all_comments = $stmt->fetchAll(PDO::FETCH_ASSOC); // Organize comments and replies $comments = []; $replies = []; foreach ($all_comments as $c) { if ($c['parent_id'] === null) { $comments[] = $c; } else { $replies[$c['parent_id']][] = $c; } } ?> <?php include 'includes/header.php'; ?> <div class="max-w-4xl mx-auto px-4 py-8"> <div class="bg-white rounded-3xl shadow-lg overflow-hidden"> <?php if($post['featured_image']): ?> <img src="uploads/<?= htmlspecialchars($post['featured_image']) ?>" class="w-full h-96 object-cover" alt="<?= htmlspecialchars($post['title']) ?>"> <?php endif; ?> <div class="p-8 md:p-12"> <div class="flex items-center gap-4 text-sm mb-6"> <span class="bg-green-100 text-green-800 px-4 py-1 rounded-full"><?= htmlspecialchars($post['category_name']) ?></span> <span><?= date('F j, Y • g:i A', strtotime($post['created_at'])) ?></span> </div> <h1 class="text-4xl font-bold leading-tight"><?= htmlspecialchars($post['title']) ?></h1> <!-- Stats --> <div class="flex gap-8 mt-6 text-gray-600 border-b pb-6"> <div><i class="fas fa-eye"></i> <strong><?= number_format($post['views']) ?></strong> Views</div> <div><i class="fas fa-heart text-red-600"></i> <strong><?= number_format($post['likes']) ?></strong> Likes</div> </div> <!-- Content --> <div class="prose prose-lg max-w-none mt-10 leading-relaxed"> <?= $post['content'] ?> </div> <!-- Like Button --> <div class="mt-12 flex justify-center"> <form method="POST"> <button type="submit" name="like" class="flex items-center gap-3 bg-red-50 hover:bg-red-100 text-red-700 px-12 py-5 rounded-2xl text-xl font-semibold transition"> ❤️ Like this Article (<?= number_format($post['likes']) ?>) </button> </form> </div> </div> </div> <!-- Comments Section --> <div class="mt-12 bg-white rounded-3xl shadow p-8"> <h3 class="text-2xl font-bold mb-6">💬 Discussions (<?= count($all_comments) ?>)</h3> <?php if($comment_success): ?> <div class="bg-green-100 text-green-700 p-4 rounded-2xl mb-6">✅ Your comment has been posted successfully!</div> <?php endif; ?> <!-- Comment Form --> <form method="POST" class="mb-12 bg-gray-50 p-6 rounded-2xl"> <input type="hidden" name="parent_id" id="parent_id" value=""> <div class="grid md:grid-cols-2 gap-4"> <input type="text" name="name" placeholder="Your Name *" required class="border border-gray-300 rounded-2xl px-5 py-4"> <input type="email" name="email" placeholder="Email (optional)" class="border border-gray-300 rounded-2xl px-5 py-4"> </div> <textarea name="comment_text" id="comment_text" rows="5" placeholder="Write your comment..." required class="w-full mt-4 border border-gray-300 rounded-3xl px-5 py-4"></textarea> <button type="submit" name="comment" class="mt-6 bg-green-800 hover:bg-green-900 text-white px-10 py-4 rounded-2xl"> Post Comment </button> </form> <!-- Display Comments --> <?php if(empty($comments)): ?> <p class="text-gray-500 italic text-center py-8">No comments yet. Be the first to comment!</p> <?php else: ?> <?php foreach($comments as $comment): ?> <div class="border-b pb-8 mb-8"> <div class="flex justify-between"> <strong><?= htmlspecialchars($comment['name']) ?></strong> <small class="text-gray-500"><?= date('M j, Y', strtotime($comment['created_at'])) ?></small> </div> <p class="mt-3 text-gray-700"><?= nl2br(htmlspecialchars($comment['comment'])) ?></p> <button onclick="replyTo(<?= $comment['id'] ?>, '<?= addslashes($comment['name']) ?>')" class="text-green-700 text-sm mt-3 hover:underline flex items-center gap-1"> ↩ Reply </button> <!-- Replies --> <?php if(isset($replies[$comment['id']])): ?> <div class="ml-10 mt-6 border-l-2 border-gray-200 pl-6 space-y-6"> <?php foreach($replies[$comment['id']] as $reply): ?> <div> <div class="flex justify-between"> <strong><?= htmlspecialchars($reply['name']) ?></strong> <small class="text-gray-500"><?= date('M j, Y', strtotime($reply['created_at'])) ?></small> </div> <p class="mt-2 text-gray-700"><?= nl2br(htmlspecialchars($reply['comment'])) ?></p> </div> <?php endforeach; ?> </div> <?php endif; ?> </div> <?php endforeach; ?> <?php endif; ?> </div> </div> <script> function replyTo(commentId, name) { document.getElementById('parent_id').value = commentId; const textarea = document.getElementById('comment_text'); textarea.focus(); textarea.placeholder = `Replying to ${name}...`; } </script> <?php include 'includes/footer.php'; ?>