Pagination Helper for CakePHP
- March 26th, 2007
- Posted in CakePHP . PHP
- Write comment
In one of my CMS projects, I ran across a case where the user created a post of very long content that scrolled endlessly down the page. In an effort to make the content more easily readable, I created a Pagination helper that breaks that content into discrete blocks of content with "next" and "prev" links.
The visibility of the content is controlled through script.aculo.us Effects.
The benefit of using this helper is that all of the content is still on the page, for SEO.
To use this helper, all you have to do is add "[page]" markers in your content, which indicate places where you want the content to break into a new "page".
Enable the helper in your controller:
In your view, render the paginated content:
Helper
-
class PaginationHelper extends Helper
-
{
-
/**
-
* Parses a string of content for [page] blocks, and replaces them with div tags for dynamic control over
-
* content section visibility.
-
*
-
* @access public
-
* @since 1.0.0
-
* @param string $strContent The content to parse and paginate
-
* @return string The paginated content
-
*
-
**/
-
function paginateContent($strContent)
-
{
-
$strPaginated = '';
-
{
-
for($i=0;$i<count($arrSections);$i++)
-
{
-
$curIndex = $i + 1;
-
$prevIndex = $i == 0 ? 1 : $curIndex - 1;
-
-
$pageID = 'page'.$curIndex;
-
$nextPage = 'page'.$nextIndex;
-
$prevPage = 'page'.$prevIndex;
-
-
// Handle first block
-
if($i == 0)
-
{
-
$strPaginated .= '<div id="'.$pageID.'" class="contentblock">'.$arrSections[$i];
-
{
-
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$nextPage.'\');">More></a>';
-
}
-
$strPaginated .= '</div>';
-
}
-
else
-
{
-
// Hide the other blocks
-
$strPaginated .= '<div id="'.$pageID.'" class="contentblock" style="display:none;">'.$arrSections[$i];
-
-
if($curIndex <count($arrSections))
-
{
-
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$nextPage.'\');">More></a>';
-
}
-
-
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$prevPage.'\');"><Prev</a>';
-
-
$strPaginated .= '</div>';
-
}
-
}
-
-
return<<<PAGE_CODE
-
$strPaginated
-
<script type="text/javascript">
-
var currentPage = 'page1';
-
</script>
-
PAGE_CODE;
-
-
}
-
return $strContent;
-
-
} // End Function
-
-
-
} // End Class
You'll also need to add some javascript code to handle the switching of visible content blocks:
-
function switchPage(thePage)
-
{
-
if(window.currentPage)
-
{
-
if(thePage != window.currentPage)
-
{
-
new Effect.Fade(window.currentPage);
-
window.currentPage = thePage;
-
new Effect.Appear(thePage, {delay:0.5});
-
}
-
}
-
}
[tags][/tags]
















No comments yet.