smarty and pagination
Posted: Tue Apr 29, 2008 2:30 pm
I found a smart< pagination script/code which I'd like to use in my serendipity installation. I tried to incorporate it in the include/functions_entries.inc.php, but this it work. Can anybody help me?
The code is written by Jorge and can be found at http://jorgev2.com/2007/05/19/smarty-and-pagination/
Here's a copy of his posting:
One task that always annoyed me has been pagination, it seems to come up in every project and every time I write some other solution for it. Well, I finally figure I’d just write a Smarty extension to take care of it.
This code is called like this:
{paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
Where the count is the number of total items, curr is the current page, max is the maximum allowed number of displayed page links, and the url is the link to the other pages.
Anyway, below is some code, you can see it in action on KFO’s browse page (http://www.kittenfaceoff.com/browse)
<?php
$oTpl->register_function(‘paginate’, ’smarty_paginate’);
function smarty_paginate ($aParam) {
// {paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
$nPageCnt = $aParam[‘count’];
$nCurrPage = $aParam[‘curr’];
$nMaxPage = $aParam[‘max’];
$sUrl = $aParam[‘url’];
$sOut = ”;
$bDrewDots = false;
if ($nPageCnt > $nMaxPage) {
if (1 > ($nCurrPage - ($nMaxPage /2))) {
$nStart = 1;
$nEnd = $nMaxPage;
} elseif ($nPageCnt < ($nCurrPage + ($nMaxPage /2))) {
$nStart = $nPageCnt - $nMaxPage;
$nEnd = $nPageCnt;
} else {
$nStart = $nCurrPage - ($nMaxPage / 2);
$nEnd = $nCurrPage + ($nMaxPage / 2);
}//if
} else {
$nStart = 1;
$nEnd = $nPageCnt;
}//if
for ($a = $nStart; $a <= $nEnd; $a++) {
if ($a == $nCurrPage)
$sOut .= “<span class=’current’>${a}</span>”;
else
$sOut .= “<a href=’” . str_replace(‘::PAGE::’, $a, $sUrl) . “‘ title=’Go to page ${a}’>${a}</a>”;
}//for
if ($nStart > 3) {
$sOut = “
<a href=’” . str_replace(‘::PAGE::’, 1, $sUrl) . “‘ title=’Go to page 1′>1</a>
<a href=’” . str_replace(‘::PAGE::’, 2, $sUrl) . “‘ title=’Go to page 2′>2</a>
<span>…</span>” . $sOut;
}//if
if ($nEnd < ($nPageCnt - 3)) {
$sOut .= “
<span>…</span>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt - 1, $sUrl) . “‘ title=’Go to page ” . ($nPageCnt - 1) . “‘>” . ($nPageCnt-1) . “</a>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt, $sUrl) . “‘ title=’Go to page ” .$nPageCnt . “‘>” .$nPageCnt . “</a>
” ;
// die($sOut);
}//if
if ($nCurrPage == 1)
$sOut = ‘<span class=”nextprev”>« Previous</span>’ . $sOut;
else
$sOut = ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage - 1, $sUrl) . ‘” class=”nextprev” title=”Go to Previous Page”>« Previous</a>’ . $sOut;
if ($nCurrPage >= $nPageCnt)
$sOut .= ‘<span class=”nextprev”>Next »</span>’;
else
$sOut .= ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage + 1, $sUrl) . ‘” class=”nextprev” title=”Go to Next Page”>Next »</a>’;
return $sOut;
}//smarty_pagenate
?>
Thanks in advance!
The code is written by Jorge and can be found at http://jorgev2.com/2007/05/19/smarty-and-pagination/
Here's a copy of his posting:
One task that always annoyed me has been pagination, it seems to come up in every project and every time I write some other solution for it. Well, I finally figure I’d just write a Smarty extension to take care of it.
This code is called like this:
{paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
Where the count is the number of total items, curr is the current page, max is the maximum allowed number of displayed page links, and the url is the link to the other pages.
Anyway, below is some code, you can see it in action on KFO’s browse page (http://www.kittenfaceoff.com/browse)
<?php
$oTpl->register_function(‘paginate’, ’smarty_paginate’);
function smarty_paginate ($aParam) {
// {paginate count=30 curr=1 max=10 url=http://here.com/page-::PAGE::}
$nPageCnt = $aParam[‘count’];
$nCurrPage = $aParam[‘curr’];
$nMaxPage = $aParam[‘max’];
$sUrl = $aParam[‘url’];
$sOut = ”;
$bDrewDots = false;
if ($nPageCnt > $nMaxPage) {
if (1 > ($nCurrPage - ($nMaxPage /2))) {
$nStart = 1;
$nEnd = $nMaxPage;
} elseif ($nPageCnt < ($nCurrPage + ($nMaxPage /2))) {
$nStart = $nPageCnt - $nMaxPage;
$nEnd = $nPageCnt;
} else {
$nStart = $nCurrPage - ($nMaxPage / 2);
$nEnd = $nCurrPage + ($nMaxPage / 2);
}//if
} else {
$nStart = 1;
$nEnd = $nPageCnt;
}//if
for ($a = $nStart; $a <= $nEnd; $a++) {
if ($a == $nCurrPage)
$sOut .= “<span class=’current’>${a}</span>”;
else
$sOut .= “<a href=’” . str_replace(‘::PAGE::’, $a, $sUrl) . “‘ title=’Go to page ${a}’>${a}</a>”;
}//for
if ($nStart > 3) {
$sOut = “
<a href=’” . str_replace(‘::PAGE::’, 1, $sUrl) . “‘ title=’Go to page 1′>1</a>
<a href=’” . str_replace(‘::PAGE::’, 2, $sUrl) . “‘ title=’Go to page 2′>2</a>
<span>…</span>” . $sOut;
}//if
if ($nEnd < ($nPageCnt - 3)) {
$sOut .= “
<span>…</span>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt - 1, $sUrl) . “‘ title=’Go to page ” . ($nPageCnt - 1) . “‘>” . ($nPageCnt-1) . “</a>
<a href=’” . str_replace(‘::PAGE::’, $nPageCnt, $sUrl) . “‘ title=’Go to page ” .$nPageCnt . “‘>” .$nPageCnt . “</a>
” ;
// die($sOut);
}//if
if ($nCurrPage == 1)
$sOut = ‘<span class=”nextprev”>« Previous</span>’ . $sOut;
else
$sOut = ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage - 1, $sUrl) . ‘” class=”nextprev” title=”Go to Previous Page”>« Previous</a>’ . $sOut;
if ($nCurrPage >= $nPageCnt)
$sOut .= ‘<span class=”nextprev”>Next »</span>’;
else
$sOut .= ‘<a href=”‘ . str_replace(‘::PAGE::’, $nCurrPage + 1, $sUrl) . ‘” class=”nextprev” title=”Go to Next Page”>Next »</a>’;
return $sOut;
}//smarty_pagenate
?>
Thanks in advance!