The pagination class makes easy to generate pagination links.
You create a pagination object by simply calling the constructor.
$pagination = new Pagination();
The offset method calculates and returns the offset you need for your database queries. The second parameter should be equal to the limit in your query.
// Using the default $itemsPerPage setting as defined in the configuration $offset = $pagination->offset($numberOfArticles); // Using a custom value for $itemsPerPage $offset = $pagination->offset($numberOfArticles, 20);
The links method generates an associative array of page links.
$links = $pagination->links('news');
// Generates links with query string (http://example.org/news/?sort_by=date)
$links = $pagination->links('news', array('sort_by' => 'date'));
$pagination = new Pagination();
$offset = $pagination->offset($news->countArticles(), 20);
$articles = $news->offset($offset, 20);
$view = new View('articles', array('articles' => $articles));
$view->page_links = new View('pagination', array('links' => $pagination->links('news')));
echo $view;
The pagination view would look something like this:
<strong><?php echo $links['num_pages']; ?></strong> <?php if(isset($links['first_page'])): ?> <a href="<?php echo $links['first_page']['url']; ?>"><?php echo $links['first_page']['name']; ?></a> <a href="<?php echo $links['previous_page']['url']; ?>"><?php echo $links['previous_page']['name']; ?></a> <?php endif; ?> <?php if(isset($links['pages'])): ?> <?php foreach($links['pages'] as $page): ?> <?php if($page['is_current']): ?> <strong><?php echo $page['name']; ?></strong> <?php else: ?> <a href="<?php echo $page['url']; ?>"><?php echo $page['name']; ?> <?php endif; ?> <?php endforeach; ?> <?php endif; ?> <?php if(isset($links['last_page'])): ?> <a href="<?php echo $links['next_page']['url']; ?>"><?php echo $links['next_page']['name']; ?></a> <a href="<?php echo $links['last_page']['url']; ?>"><?php echo $links['last_page']['name']; ?></a> <?php endif; ?>