-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
68 lines (60 loc) · 2.22 KB
/
index.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
<?php
require_once 'services/db.php';
require_once 'services/users.php';
$db = OpenCon();
$categories = $db->query('SELECT * FROM categories;')->fetchAll();
$raw_posts_queries = array();
foreach($categories as $cat) {
$cat_posts_query = '(SELECT * FROM posts WHERE category_id = ' . $cat['id'] . ' ORDER BY id ASC LIMIT 4)';
array_push($raw_posts_queries, $cat_posts_query);
}
$full_cat_query = (implode(' UNION ', $raw_posts_queries));
$result = $db->query($full_cat_query)->fetchAll();
$categories_top_posts = array();
foreach($categories as $cat) {
$posts_of_cat = array_filter($result, function($post) {
global $cat;
return ($post['category_id'] == $cat['id']);
});
$categories_top_posts[$cat['id']] = array(
'category' => $cat,
'posts' => $posts_of_cat
);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require 'components/base_head.php' ?>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<?php require 'components/navbar.php' ?>
<script src='js/navbar.js'></script>
<script>
navbarSetActiveButton('home_link')
</script>
<div class="col-md-5 p-lg-5 mx-auto my-5">
<h1 class="display-4 fw-normal">My Awesome Blog</h1>
<p class="lead fw-normal">This is my simple PHP blog engine.</p>
<a class="btn btn-outline-dark" href="#">Get on GitHub</a>
</div>
<div class="container mt-2">
<?php foreach($categories_top_posts as $cat): ?>
<div class="category_block mt-5">
<a href="/category_details.php?category_id=<?=$cat['category']['id']?>" class="text-decoration-none">
<h2><?=$cat['category']['title']?></h2>
</a>
<p class="text-muted text-uppercase fw-light">Последниe посты</p>
<div class="row">
<?php foreach($cat['posts'] as $post): ?>
<?php require 'components/post_card.php' ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php require 'components/footer.php' ?>
<?php require 'components/base_scripts.php' ?>
</body>
</html>