51 lines
2.1 KiB
PHP
51 lines
2.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$posts = [];
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query("SELECT posts.*, users.username FROM posts JOIN users ON posts.user_id = users.id ORDER BY posts.created_at DESC");
|
|
$posts = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
// Handle error
|
|
echo "<div class='alert alert-danger'>Error fetching posts: " . $e->getMessage() . "</div>";
|
|
}
|
|
?>
|
|
|
|
<div class="container" style="margin-top: 100px;">
|
|
<div class="row">
|
|
<div class="col-md-12">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>Community Feed</h1>
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<a href="create_post.php" class="btn btn-primary">Create Post</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if (empty($posts)): ?>
|
|
<div class="card text-center">
|
|
<div class="card-body">
|
|
<p class="card-text">No posts yet. Be the first to share your story!</p>
|
|
<?php if (!isset($_SESSION['user_id'])): ?>
|
|
<a href="login.php" class="btn btn-primary">Login to Post</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($posts as $post): ?>
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h2 class="card-title"><?php echo htmlspecialchars($post['title']); ?></h2>
|
|
<p class="card-text"><?php echo nl2br(htmlspecialchars($post['content'])); ?></p>
|
|
<p class="card-text"><small class="text-muted">Posted by <?php echo htmlspecialchars($post['username']); ?> on <?php echo date('F j, Y', strtotime($post['created_at'])); ?></small></p>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|