-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
91 lines (80 loc) · 3.57 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
$filename = __DIR__ . '/data/articles.json';
$articles = [];
$categories = [];
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$selectedCat = $_GET['cat'] ?? '';
if (file_exists($filename)) {
$articles = json_decode(file_get_contents($filename), true) ?? [];
$cattmp = array_map(fn ($a) => $a['category'], $articles);
$categories = array_reduce($cattmp, function ($acc, $cat) {
if (isset($acc[$cat])) {
$acc[$cat]++;
} else {
$acc[$cat] = 1;
}
return $acc;
}, []);
$articlePerCategories = array_reduce($articles, function ($acc, $article) {
if (isset($acc[$article['category']])) {
$acc[$article['category']] = [...$acc[$article['category']], $article];
} else {
$acc[$article['category']] = [$article];
}
return $acc;
}, []);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once 'includes/head.php' ?>
<link rel="stylesheet" href="/public/css/index.css">
<title>Blog</title>
</head>
<body>
<div class="container">
<?php require_once 'includes/header.php' ?>
<div class="content">
<div class="newsfeed-container">
<ul class="category-container">
<li class=<?= $selectedCat ? '' : 'cat-active' ?>><a href="/">Tous les articles <span class="small">(<?= count($articles) ?>)</span></a></li>
<?php foreach ($categories as $catName => $catNum) : ?>
<li class=<?= $selectedCat === $catName ? 'cat-active' : '' ?>><a href="/?cat=<?= $catName ?>"> <?= $catName ?><span class="small">(<?= $catNum ?>)</span> </a></li>
<?php endforeach; ?>
</ul>
<div class="newsfeed-content">
<?php if (!$selectedCat) : ?>
<?php foreach ($categories as $cat => $num) : ?>
<h2><?= $cat ?></h2>
<div class="articles-container">
<?php foreach ($articlePerCategories[$cat] as $a) : ?>
<a href="/show-article.php?id=<?= $a['id'] ?>" class="article block">
<div class="overflow">
<div class="img-container" style="background-image:url(<?= $a['image'] ?>"></div>
</div>
<h3><?= $a['title'] ?></h3>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2><?= $selectedCat ?></h2>
<div class="articles-container">
<?php foreach ($articlePerCategories[$selectedCat] as $a) : ?>
<a href="/show-article.php?id=<?= $a['id'] ?>" class="article block">
<div class="overflow">
<div class="img-container" style="background-image:url(<?= $a['image'] ?>"></div>
</div>
<h3><?= $a['title'] ?></h3>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php require_once 'includes/footer.php' ?>
</div>
</body>
</html>