-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories-post.php
87 lines (80 loc) · 3.44 KB
/
categories-post.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
86
87
<?php
include 'Partials/header.php';
//fetch posts from databse if id is set
if(isset($_GET['id'])) {
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
$query = "SELECT * FROM posts WHERE category_id=$id ORDER BY date_time DESC";
$result = mysqli_query($connection, $query);
} else {
header('location: ' . ROOT_URL . 'blog.php');
die();
}
?>
<?php
//fetch categories teble using category id of post
$category_id = $id;
$category_query = "SELECT * FROM categories WHERE id=$category_id";
$category_result = mysqli_query($connection, $category_query);
$category = mysqli_fetch_assoc($category_result);
?>
<header class="category-title">
<h2><?= $category['title'] ?></h2>
</header>
<!-- ============================END OF CATEGORY TITLE============================ -->
<?php if (mysqli_num_rows($result) > 0) : ?>
<section class="posts">
<div class="container posts-container">
<?php while($post =mysqli_fetch_assoc($result)) : ?>
<article class="post">
<div class="post-thumbnail">
<img src="./images/<?= $post['thumbnail'] ?>">
</div>
<div class="post-info">
<h3 class="post-title">
<a href="<?= ROOT_URL?>post.php?id=<?= $post['id'] ?>"><?= $post['title'] ?></a>
</h3>
<p class="post-body">
<?= substr($post['body'], 0, 150) ?>...
</p>
<div class="post-author">
<?php
//fetch author from users table using the author_id
$author_id = $post['author_id'];
$author_query = "SELECT * FROM users WHERE id=$author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_assoc($author_result);
?>
<div class="post-author-avatar">
<img src="./images/<?= $author['avatar'] ?>">
</div>
<div class="post-author-info">
<h5>By: <?= "{$author['firstname']} {$author['lastname']}" ?></h5>
<small><?= date("M d, Y - H:i", strtotime($post['date_time'])) ?></small>
</div>
</div>
</div>
</article>
<?php endwhile ?>
</div>
</section>
<?php else : ?>
<div class="alert-message error lg">
<p>No posts found for this category</p>
</div>
<?php endif ?>
<!-- ============================END OF POSTS============================ -->
<section class="category-buttons">
<div class="container category-buttons-container">
<?php
$all_categories_query = "SELECT * FROM categories";
$all_categories = mysqli_query($connection, $all_categories_query);
?>
<?php while($category = mysqli_fetch_assoc($all_categories)) : ?>
<a href="<?= ROOT_URL?>categories-post.php?id=<?= $category['id'] ?>" class="category-button"><?= $category['title'] ?></a>
<?php endwhile ?>
</div>
</section>
<!-- ============================END OF CATEGORY BUTTONS ============================ -->
<?php
include 'Partials/footer.php'
?>