diff options
Diffstat (limited to 'lib/partials.php')
| -rw-r--r-- | lib/partials.php | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/lib/partials.php b/lib/partials.php index 2d6cd44..12aa23d 100644 --- a/lib/partials.php +++ b/lib/partials.php @@ -162,23 +162,59 @@ function html_navigation_search() function html_pagination(int $total_pages, int $current_page, string $redirect) { + if ($total_pages < 1) { + return; + } + if (str_contains($redirect, "?")) { $redirect .= "&p="; } else { $redirect .= "?p="; } - if ($total_pages > 1) { - echo '' ?> - <div class="pagination"> - <?php if ($current_page > 1): ?> - <a href="<?= $redirect . ($current_page - 1) ?>">[ prev ]</a> - <?php endif; ?> - <?php if ($current_page < $total_pages): ?> - <a href="<?= $redirect . ($current_page + 1) ?>">[ next ]</a> - <?php endif; ?> + echo '<div class="pagination">'; - </div> - <?php ; + if ($current_page > 1) { + echo '<a href="' . $redirect . ($current_page - 1) . '"><</a>'; + } + + echo "<a href='{$redirect}1'>" . ($current_page == 1 ? "<b>1</b>" : 1) . '</a>'; + + $max = 5; + $range = 2; + + $start = max(2, $current_page - $range); + $end = min($total_pages - 1, $current_page + $range); + + if ($current_page <= $range) { + $start = 2; + $end = min($total_pages - 1, $max); + } + + if ($current_page > $total_pages - $range) { + $start = max(2, $total_pages - $max); + $end = $total_pages - 1; + } + + if ($start > 2) { + echo '...'; } + + for ($i = $start; $i <= $end; $i++) { + echo "<a href='{$redirect}$i'>"; + echo $i == $current_page ? "<b>$i</b>" : $i; + echo '</a>'; + } + + if ($end < $total_pages - 1) { + echo '...'; + } + + echo "<a href='{$redirect}$total_pages'>" . ($current_page == $total_pages ? "<b>$total_pages</b>" : $total_pages) . '</a>'; + + if ($current_page < $total_pages) { + echo '<a href="' . $redirect . ($current_page + 1) . '">></a>'; + } + + echo '</div>'; }
\ No newline at end of file |
