-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
120 lines (109 loc) · 5.1 KB
/
index.php
File metadata and controls
120 lines (109 loc) · 5.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
include 'partials/header.php';
// fetch featured post from database
$featured_query = "SELECT * FROM posts WHERE is_featured='1'";
$featured_result = mysqli_query($connection, $featured_query);
$featured = mysqli_fetch_array($featured_result);
// fetch 9 posts from posts table
$query = "SELECT * FROM posts ORDER BY date_time DESC LIMIT 9";
$posts = mysqli_query($connection, $query);
?>
<!-- Show featured post if there is any -->
<?php if (mysqli_num_rows($featured_result) == 1) : ?>
<section class="featured">
<div class="container featured__container">
<div class="post__thumbnail">
<img src="./images/<?= $featured['thumbnail'] ?>">
</div>
<div class="post__info">
<?php
// fetch category from categories table using category id
$category_id = $featured['category_id'];
$category_query = "SELECT * FROM categories WHERE id=$category_id";
$category_result = mysqli_query($connection, $category_query);
$category = mysqli_fetch_array($category_result);
?>
<a href="<?= ROOT_URL ?>category-posts.php?id=<?= $category_id ?>" class="category__button"><?= $category['title'] ?></a>
<h2 class="post__title"><a href="<?= ROOT_URL ?>post.php?id=<?= $featured['id'] ?>"><?= $featured['title']?></a></h2>
<p class="post__body">
<?= substr($featured['body'], 0, 300)?>...
</p>
<div class="post__author">
<?php
// fetch category from categories table using category id
$author_id = $featured['author_id'];
$author_query = "SELECT * FROM users WHERE id=$author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_array($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($featured['date_time'])) ?></small>
</div>
</div>
</div>
</div>
</section>
<?php endif ?>
<!--======================= END OF FEATURE ==========================-->
<section class="posts <?= $featured ? '' : 'section__extra-margin' ?>">
<div class="container posts__container">
<?php while ($post = mysqli_fetch_assoc($posts)) : ?>
<article class="post">
<div class="post__thumbnail">
<img src="./images/<?= $post['thumbnail'] ?>">
</div>
<div class="post__info">
<?php
// fetch category from categories table using category id
$category_id = $post['category_id'];
$category_query = "SELECT * FROM categories WHERE id=$category_id";
$category_result = mysqli_query($connection, $category_query);
$category = mysqli_fetch_array($category_result);
?>
<a href="<?= ROOT_URL ?>category-posts.php?id=<?= $category_id ?>" class="category__button"><?= $category['title'] ?></a>
<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 category from categories table using category 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_array($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>
<!--======================= END OF POSTS ==========================-->
<section class="category__buttons">
<div class="container category__buttons-container">
<?php
$all_categories_query = "SELECT * FROM categories";
$all_categories_result = mysqli_query($connection, $all_categories_query);
?>
<?php while($category = mysqli_fetch_assoc($all_categories_result)) : ?>
<a href="<?= ROOT_URL ?>category-posts.php?id=<?= $category['id'] ?>" class="category__button"><?= $category['title']?></a>
<?php endwhile ?>
</div>
</section>
<!--======================= END OF CATEGORY BUTTONS ==========================-->
<?php
include 'partials/footer.php';
?>