Pagnations done

This commit is contained in:
sørenpeter 2025-01-31 20:35:04 +01:00
parent b65c496c0d
commit 6e46dd04ae
4 changed files with 54 additions and 84 deletions

View file

@ -1,83 +1,52 @@
<?php <?php
/* $maxTwts = $config['twts_per_page'] ?? 50; // Fallback number if twts_per_page not set in config.ini
TODO:
+ Test if $twts are bigger/lowe than max to detemen if pagnations is needed
+ Make pagnation work with profile views
- Move twts per page to config.ini
- Add a fallback if the number is invalid (it should be between 1 and 999)
//$twts_per_page = $config['twts_per_page'];
*/
const TWTS_PER_PAGE = 20;
$twts_per_page = TWTS_PER_PAGE;
$totalTwts = count($twts); $totalTwts = count($twts);
// Test if pagnation is needed and set stat if ( ($maxTwts >= $totalTwts) || ($paginateTwts == false) ) {
// TODO: consider how to have /conv alway show all twts // echo "Only " . $totalTwts . " so no need for pagnation. (max: ". $maxTwts . ")<br>";
if ( $twts_per_page >= $totalTwts ) {
//echo "Only " . $totalTwts . " so no need for pagnation. (max: ". $twts_per_page . ")<br>";
$paginateTwts = false; $paginateTwts = false;
} else {
//echo "Over " . $twts_per_page . " so pagnation is needed.<br>";
$paginateTwts = true;
}
$page = 1;
if (!empty($_GET['page'])) {
$page = intval($_GET['page']);
}
// If we should paginate our twts list
if (!empty($paginateTwts)) {
$startingTwt = (($page - 1) * TWTS_PER_PAGE);
$twts = array_slice($twts, $startingTwt, TWTS_PER_PAGE);
}
# Approach 2: Ignore invalid page numbers, and adjust the number between 1 and the last page
$currentPage = max(1, min($_GET["page"], $totalPages));
# And then in the HTML template only show the next page link if we are NOT on the last one
if ($currentPage < $totalPages) {
$output .= '<a href="?page=' . ($currentPage + 1) . '">&gt; Next Page</a>';
}
$totalTwts = count($twts);
//echo "pagnation twts: " . $totalTwts;
$totalPages = ceil($totalTwts / $twts_per_page);
//echo "<br>total pages: " . $totalPages;
$page_url = $_SERVER['REQUEST_URI'];
//echo $page_url."<hr>";
if (!empty($_GET['page'])) {
if (preg_match('/\?page=/', $page_url)) {
//echo "cotains ?page= so reuse that <hr>";
$page_url = preg_replace('/\?page=\d*/', "", $page_url) . '?page=';
}
if (preg_match('/&page=/', $page_url)) {
//echo "cotains &page= so reuse that <hr>";
$page_url = preg_replace('/&page=\d*/', "", $page_url) . '&page=';
}
} else { } else {
// echo "Over " . $maxTwts . " so pagnation is needed.<br>";
$paginateTwts = true; // for showing pagnation navigation below timeline
if (!preg_match('/(\?|&)/', $page_url)) { $totalPages = ceil($totalTwts / $maxTwts);
//echo "No param, so use ?page <hr>";
$page_url = $page_url . '?page='; $currentPage = max(1, min($_GET["page"], $totalPages));
// Split up twts into pages
$startingTwt = (($currentPage - 1) * $maxTwts);
$twts = array_slice($twts, $startingTwt, $maxTwts);
// Fingure out base URL for prev/next links
$pageURL = $_SERVER['REQUEST_URI'];
//echo $pageURL."<hr>";
if (!empty($_GET['page'])) {
if (preg_match('/\?page=/', $pageURL)) {
//echo "cotains ?page= so reuse that <hr>";
$pageURL = preg_replace('/\?page=\d*/', "", $pageURL) . '?page=';
}
if (preg_match('/&page=/', $pageURL)) {
//echo "cotains &page= so reuse that <hr>";
$pageURL = preg_replace('/&page=\d*/', "", $pageURL) . '&page=';
}
} else {
if (!preg_match('/(\?|&)/', $pageURL)) {
//echo "No param, so use ?page <hr>";
$pageURL = $pageURL . '?page=';
}
if (!preg_match('/(\?|&)page/', $pageURL)) {
//echo "other param than _page, so use &page <hr>";
$pageURL = $pageURL . '&page=';
}
} }
if (!preg_match('/(\?|&)page/', $page_url)) {
//echo "other param than _page, so use &page <hr>";
$page_url = $page_url . '&page=';
}
} }
?>

View file

@ -6,7 +6,6 @@
<?php }?> <?php }?>
--> -->
<?php <?php
include_once 'partials/search.php'; include_once 'partials/search.php';
@ -60,19 +59,21 @@ include_once 'partials/pagnation.php';
if ($paginateTwts) { ?> if ($paginateTwts) { ?>
<div class="pagnation"> <div class="pagnation">
<?php if ($page > 1) { ?> <?php if ($currentPage> 1) { ?>
<a href="<?= $page_url . $page-1 ?>"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> <a href="<?= $pageURL . $currentPage- 1 ?>"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>
<?php } else { ?> <?php } else { ?>
<span style="color: var(--disabled);"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</span> <span style="color: var(--disabled);"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</span>
<?php } ?> <?php } ?>
<strong>&nbsp;<?= $page ?>&nbsp;</strong> <strong>&nbsp;<?= $currentPage /* . " / " . $totalPages */ ?>&nbsp;</strong>
<a href="<?= $page_url . $page+1 ?>">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a> <?php if ($currentPage < $totalPages) { ?>
<a href="<?= $pageURL . $currentPage+1 ?>">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a>
<?php } else { ?>
<span style="color: var(--disabled);">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></span>
<?php } ?>
</div> </div>
<?php } <?php }
require_once 'libs/session.php'; if (!hasValidSession() && isset($config['email']) ) {
echo '<center><a href="mailto:' . $config['email'] . '?subject=RE: ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '" class="button">Reply via email</a></center>';
if (!hasValidSession()) {
echo '<center><a href="mailto:' . $config['email'] . '?subject=RE: ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '" class="button">Comment via email</a></center>';
} }

View file

@ -1,6 +1,6 @@
<?php <?php
//$paginateTwts = true; $paginateTwts = true;
if (!empty($_GET['url'])) { // Show twts for some user (Profile view) if (!empty($_GET['url'])) { // Show twts for some user (Profile view)
$twtsURL = $_GET['url']; $twtsURL = $_GET['url'];

View file

@ -1,6 +1,6 @@
<?php <?php
//$paginateTwts = true; $paginateTwts = true;
if (!empty($_GET['url'])) { // Show twts for some user (Profile view) if (!empty($_GET['url'])) { // Show twts for some user (Profile view)
$twtsURL = $_GET['url']; $twtsURL = $_GET['url'];