Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

limiting pagination

     
2:55 am on May 20, 2008 (gmt 0)

New User

10+ Year Member

joined:Nov 9, 2007
posts: 24
votes: 0


Hi everyone, hope you're well :)
I need to limit the pagination of a couple of my websites. Since I'm no pro in php, I thought i'd post the part which I think needs to be fixed.
If someone could look at it and let me know if there is a fix -- that would be amazing :)
thanks!

/**
* Build a set of pagination links for large result sets.
*
* @param int The number of results
* @param int The number of results per page
* @param int The current page
* @param string The base URL to add page numbers to
* @return string The built pagination
*/
function BuildPagination($resultCount, $perPage, $currentPage, $url)
{
if ($resultCount <= $perPage) {
return;
}

$pageCount = ceil($resultCount / $perPage);
$pagination = '';

if ($currentPage > 1) {
$pagination .= sprintf("<a href='%s'>&laquo;&laquo;</a> ¦", BuildPaginationUrl($url, 1));
$pagination .= sprintf(" <a href='%s'>&laquo; %s</a> ¦", BuildPaginationUrl($url, $currentPage - 1), GetLang('Prev'));
}
else {
$pagination .= '&laquo;&nbsp;' . GetLang('Prev') . '&nbsp;¦';
}

$MaxLinks = 10;

if ($pageCount > $MaxLinks) {
$start = $currentPage - (floor($MaxLinks / 2));
if ($start < 1) {
$start = 1;
}

$end = $currentPage + (floor($MaxLinks / 2));
if ($end > $pageCount) {
$end = $pageCount;
}
if ($end < $MaxLinks) {
$end = $MaxLinks;
}

$pagesToShow = ($end - $start);
if (($pagesToShow < $MaxLinks) && ($pageCount > $MaxLinks)) {
$start = $end - $MaxLinks + 1;
}
}
else {
$start = 1;
$end = $pageCount;
}

for ($i = $start; $i <= $end; ++$i) {
if ($i > $pageCount) {
break;
}

$pagination .= '&nbsp;';
if ($i == $currentPage) {
$pagination .= sprintf(" <strong>%d</strong> ¦", $i);
} else {
$pagination .= sprintf(" <a href='%s'>%d</a> ¦", BuildPaginationUrl($url, $i), $i);
}
}

if ($currentPage == $pageCount) {
$pagination .= '&nbsp;' . GetLang('Next') . '&nbsp;&raquo;';
} else {
$pagination .= sprintf(" <a href='%s'>%s &raquo;</a> ¦", BuildPaginationUrl($url, $currentPage + 1), GetLang('Next'));
$pagination .= sprintf(" <a href='%s'>&raquo;&raquo;</a>", BuildPaginationUrl($url, $pageCount));
}

return $pagination;
}

function BuildPaginationUrl($url, $page)
{
if (isc_strpos($url, "{page}") === false) {
if (isc_strpos($url, "?") === false) {
$url .= "?";
}
else {
$url .= "&amp;";
}
$url .= "page=$page";
}
else {
$url = str_replace("{page}", $page, $url);
}
return $url;
}

6:23 am on May 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Hi,

When you say you need to limt it, limit it to what?

dc