News pagination

This commit is contained in:
rigaer@hotmail.es 2014-05-17 12:35:45 +02:00
parent 6f2a6049ba
commit 48d6483a82
2 changed files with 51 additions and 14 deletions

View File

@ -200,8 +200,13 @@
// -------------- \\ // -------------- \\
// WEBSITE STUFF \\ // WEBSITE STUFF \\
// -------------- \\ // -------------- \\
// News to be displayed per page
$config['news_per_page'] = 4;
// Enable or disable changelog ticker in news page. // Enable or disable changelog ticker in news page.
$config['UseChangelogTicker'] = true; $config['UseChangelogTicker'] = true;
// Highscore configuration // Highscore configuration
$config['highscore'] = array( $config['highscore'] = array(
'rows' => 100, 'rows' => 100,

View File

@ -1,4 +1,11 @@
<?php require_once 'engine/init.php'; include 'layout/overall/header.php'; <?php require_once 'engine/init.php'; include 'layout/overall/header.php';
if (!isset($_GET['page'])) {
$page = 0;
} else {
$page = (int)$_GET['page'];
}
if ($config['allowSubPages'] && file_exists("layout/sub/index.php")) include 'layout/sub/index.php'; if ($config['allowSubPages'] && file_exists("layout/sub/index.php")) include 'layout/sub/index.php';
else { else {
if ($config['UseChangelogTicker']) { if ($config['UseChangelogTicker']) {
@ -32,7 +39,6 @@
$cache = new Cache('engine/cache/news'); $cache = new Cache('engine/cache/news');
if ($cache->hasExpired()) { if ($cache->hasExpired()) {
$news = fetchAllNews(); $news = fetchAllNews();
$cache->setContent($news); $cache->setContent($news);
$cache->save(); $cache->save();
} else { } else {
@ -41,6 +47,12 @@
// Design and present the list // Design and present the list
if ($news) { if ($news) {
$total_news = count($news);
$row_news = $total_news / $config['news_per_page'];
$page_amount = ceil($total_news / $config['news_per_page']);
$current = $config['news_per_page'] * $page;
function TransformToBBCode($string) { function TransformToBBCode($string) {
$tags = array( $tags = array(
'[center]{$1}[/center]' => '<center>$1</center>', '[center]{$1}[/center]' => '<center>$1</center>',
@ -58,20 +70,40 @@
} }
return $string; return $string;
} }
foreach ($news as $n) { for ($i = $current; $i < $current + $config['news_per_page']; $i++) {
?> if (isset($news[$i])) {
<table id="news"> ?>
<tr class="yellow"> <table id="news">
<td class="zheadline"><?php echo getClock($n['date'], true) .' by <a href="characterprofile.php?name='. $n['name'] .'">'. $n['name'] .'</a> - <b>'. TransformToBBCode($n['title']) .'</b>'; ?></td> <tr class="yellow">
</tr> <td class="zheadline"><?php echo getClock($news[$i]['date'], true) .' by <a href="characterprofile.php?name='. $news[$i]['name'] .'">'. $news[$i]['name'] .'</a> - <b>'. TransformToBBCode($news[$i]['title']) .'</b>'; ?></td>
<tr> </tr>
<td> <tr>
<p><?php echo TransformToBBCode(nl2br($n['text'])); ?></p> <td>
</td> <p><?php echo TransformToBBCode(nl2br($news[$i]['text'])); ?></p>
</tr> </td>
</table> </tr>
<?php </table>
<?php
}
} }
echo '<select name="newspage" onchange="location = this.options[this.selectedIndex].value;">';
for ($i = 0; $i < $page_amount; $i++) {
if ($i == $page) {
echo '<option value="index.php?page='.$i.'" selected>Page '.$i.'</option>';
} else {
echo '<option value="index.php?page='.$i.'">Page '.$i.'</option>';
}
}
echo '</select>';
} else { } else {
echo '<p>No news exist.</p>'; echo '<p>No news exist.</p>';
} }