Update index.php

This commit is contained in:
deepend-tildeclub 2024-08-17 08:49:36 -06:00 committed by GitHub
parent 8c0ccfb1fb
commit c115370510
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 54 additions and 22 deletions

View File

@ -13,8 +13,29 @@
<?php <?php
$news = json_decode(file_get_contents('news.json'), true); $news = json_decode(file_get_contents('news.json'), true);
// Sort news items by date, most recent first
usort($news, function ($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
// Determine the year filter if present
$selectedYear = isset($_GET['year']) ? $_GET['year'] : null;
$filteredNews = [];
if ($selectedYear) {
// Filter news by selected year
foreach ($news as $newsItem) { foreach ($news as $newsItem) {
if (new DateTime() >= new DateTime($newsItem['date'])) { if (date('Y', strtotime($newsItem['date'])) == $selectedYear) {
$filteredNews[] = $newsItem;
}
}
} else {
// Show only the most recent 2 news items if no year filter is applied
$filteredNews = array_slice($news, 0, 2);
}
// Display news items
foreach ($filteredNews as $newsItem) {
echo '<h2>' . htmlspecialchars($newsItem['title']) . ':</h2>'; echo '<h2>' . htmlspecialchars($newsItem['title']) . ':</h2>';
echo '<h3>' . htmlspecialchars($newsItem['heading']) . '</h3>'; echo '<h3>' . htmlspecialchars($newsItem['heading']) . '</h3>';
echo '<p>' . htmlspecialchars($newsItem['content']) . '</p>'; echo '<p>' . htmlspecialchars($newsItem['content']) . '</p>';
@ -37,7 +58,18 @@
echo '<hr>'; echo '<hr>';
} }
// Display year links for filtering
$years = array_unique(array_map(function ($newsItem) {
return date('Y', strtotime($newsItem['date']));
}, $news));
sort($years);
echo '<div><strong>View news by year:</strong> ';
foreach ($years as $year) {
echo '<a href="?year=' . $year . '">' . $year . '</a> ';
} }
echo '</div>';
?> ?>
</div> </div>