mirror of https://github.com/tildeclub/site.git
Update index.php
This commit is contained in:
parent
8c0ccfb1fb
commit
c115370510
34
index.php
34
index.php
|
@ -13,8 +13,29 @@
|
|||
<?php
|
||||
$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) {
|
||||
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 '<h3>' . htmlspecialchars($newsItem['heading']) . '</h3>';
|
||||
echo '<p>' . htmlspecialchars($newsItem['content']) . '</p>';
|
||||
|
@ -37,7 +58,18 @@
|
|||
|
||||
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>
|
||||
|
||||
|
|
Loading…
Reference in New Issue