34 lines
1.1 KiB
PHP
34 lines
1.1 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
// Check if there are any users in the database
|
|
$stmt = db()->query("SELECT id FROM users LIMIT 1");
|
|
if ($stmt->rowCount() === 0) {
|
|
header('Location: register.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'includes/header.php';
|
|
|
|
$stmt = db()->query("SELECT posts.*, users.username FROM posts JOIN users ON posts.user_id = users.id ORDER BY posts.created_at DESC");
|
|
$posts = $stmt->fetchAll();
|
|
?>
|
|
|
|
<h1>Blog</h1>
|
|
|
|
<div class="row">
|
|
<?php foreach ($posts as $post): ?>
|
|
<div class="col-md-4 mb-4">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?php echo htmlspecialchars($post['title']); ?></h5>
|
|
<p class="card-text">By <?php echo htmlspecialchars($post['username']); ?> on <?php echo date('F j, Y', strtotime($post['created_at'])); ?></p>
|
|
<a href="post.php?slug=<?php echo $post['slug']; ?>" class="btn btn-primary">Read More</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|