aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaquer <jaquer@users.noreply.github.com>2022-01-26 12:46:15 -0800
committerjaquer <jaquer@users.noreply.github.com>2022-01-26 12:46:15 -0800
commitbc96650b4c09cb3d44b3cd33607d3e7f363ad8fb (patch)
tree0328c18a1502ba99966f79a81c6c2847df9e8c12
parentde148409fe5024e662ee79a9d6a67368b727ccfb (diff)
downloadmicroblog-bc96650b4c09cb3d44b3cd33607d3e7f363ad8fb.tar.gz
microblog-bc96650b4c09cb3d44b3cd33607d3e7f363ad8fb.tar.bz2
microblog-bc96650b4c09cb3d44b3cd33607d3e7f363ad8fb.zip
Improved pagination.
Link for 'older/newer updates' pages only show if there's more pages to be shown in their corresponding directions.
-rw-r--r--functions.php12
-rw-r--r--microblog.css1
-rw-r--r--timeline.inc.php8
3 files changed, 16 insertions, 5 deletions
diff --git a/functions.php b/functions.php
index 19325d1..5941ed8 100644
--- a/functions.php
+++ b/functions.php
@@ -62,6 +62,18 @@ function db_select_posts($from=NOW, $amount=10, $sort='desc', $page=1) {
return (!empty($rows)) ? $rows : false;
}
+function db_posts_count() {
+ global $config;
+ global $db;
+ if(empty($db)) return false;
+
+ $statement = $db->prepare('SELECT COUNT(*) AS posts_count FROM posts');
+ $statement->execute();
+ $row = $statement->fetch(PDO::FETCH_ASSOC);
+
+ return (int) $row['posts_count'];
+}
+
/* function that pings the official micro.blog endpoint for feed refreshes */
function ping_microblog() {
global $config;
diff --git a/microblog.css b/microblog.css
index 0f46fdf..711cb67 100644
--- a/microblog.css
+++ b/microblog.css
@@ -71,7 +71,6 @@ nav li + li a {
font-weight: bold;
float: left;
}
-.timeline .pagination a.disabled { background-color: #b5b5af; }
.timeline .pagination .next { float: right; }
.wrap .post-timestamp {
diff --git a/timeline.inc.php b/timeline.inc.php
index c36b3bd..26b90bf 100644
--- a/timeline.inc.php
+++ b/timeline.inc.php
@@ -10,8 +10,8 @@
// pagination
$current_page = (path(0) == 'page' && is_numeric(path(1))) ? (int) path(1) : 1;
- $previous_page = ($current_page > 1) ? $current_page-1 : 1;
- $next_page = $current_page + 1;
+ $posts_count = db_posts_count();
+ $total_pages = ceil($posts_count / $config['posts_per_page']);
// get posts
$posts = db_select_posts(NOW, $config['posts_per_page'], 'desc', $current_page);
@@ -49,8 +49,8 @@
<p>No posts found.</p>
<?php endif; ?>
<div class="pagination">
- <a href="<?= $config['url'] ?>/page/<?= $previous_page ?>" class="previous<?= ($current_page == 1) ? ' disabled': '' ?>">newer updates</a>
- <a href="<?= $config['url'] ?>/page/<?= $next_page ?>" class="next">older updates</a>
+ <?php if ($current_page > 1): ?><a href="<?= $config['url'] ?>/page/<?= $current_page - 1 ?>" class="previous">newer updates</a><?php endif; ?>
+ <?php if ($current_page < $total_pages): ?><a href="<?= $config['url'] ?>/page/<?= $current_page + 1 ?>" class="next">older updates</a><?php endif; ?>
</div>
</div>
</body>