I was wondering if anybody could help me out with a bit of a pagination issue in my store. Using pajinate, I initially put this together but it seems to be bugged. I’d like to add a ‘previous’ and ‘back to beginning’ as well as a ‘next’ and ‘last page’ button. I tried implementing the ‘back to beginning’ and ‘previous’ buttons first but they aren’t working correctly. If anybody could help me out a bit that would be fantastic. On a side note, i have this set to display a range of 10 numbers at a time, but each time i click on a higher page number it appends with 4-5 more numbers any insight on this issue would be greatly appreciated as well.
<?php
// find out total pages
$totalpages = $resp->paginationOutput->totalPages;
echo "Total Results: ";
echo $totalpages.' pages ';
// get the current page or set a default
if (isset($_GET['pgno']) && is_numeric($_GET['pgno'])) {
// cast var as int
$currentpage = (int) $_GET['pgno'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $entriesPerPage;
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
if(null!=$queryString){
// echo " <a href='{$_SERVER['PHP_SELF']}?$queryString¤tpage=1'><<</a> ";
}else{
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
}
// get previous page num
// $prevpage = $currentpage - 1;
// show < link to go back 1 page
// echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// range of num links to show
$range = 10;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<strong>$x</strong>] ";
// if not current page...
} else {
// make it a link
echo " <a href='".add_url_param('pgno',$x)."'>$x</a> ";
} // end else
} // end if
} // end for
?>
2
Answers
you had prev page commented out, and using $_GET[‘currentpage’] rather than $_GET[‘pgno’], this worked for me: