Skip to content

pagination

David Spreekmeester edited this page Dec 2, 2014 · 1 revision

Pagination

Zend makes pagination easy thru their Zend_Paginator class. Set it up like this (from your controller):

<?php
$movieModel = new Model_Movie();
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbTableSelect(
$movieModel->select()->order('Movie.name')
)
);
$paginator->setItemCountPerPage(4);
$paginator->setCurrentPageNumber($page); 
$this->view->paginator = $paginator;
?>

Then in your view, you can loop over $paginator as if it was a rowset:

<?php
foreach ($this->paginator as $item):
	echo '<h2>'.$this->escape($item->name).'</h2>';
	echo '<p>'.$item->description.'</p>';
endforeach;

echo $this->paginationControl($this->paginator, 'Sliding', 'partials/pagination_control.phtml', $additional_params);
?>

Note that last helper call, which prints the navigation. The second parameter is the "scrolling" method, see http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.rendering for more information. The third is the partial used to print the pagination control. The fourth parameter can be used to pass additional parameters.

Here's an example of the pagination partial:

<?php
if ($this->pageCount):
?>
<div class="paginationControl">
<?php if (isset($this->previous)): ?>
	<a href="<?php echo $this->baseUrl('/index/pagination/page/'.$this->previous) ?>">&laquo; previous</a> |
<?php
	endif;

	foreach ($this->pagesInRange as $page):
		if ($page != $this->current):
?>
	<a href="<?php echo $this->baseUrl('/index/pagination/page/'.$page) ?>"><?php echo $page ?></a>
<?php
		else:
			echo $page;
		endif;
?> |
<?php
	endforeach;

	if (isset($this->next)):
?>
	<a href="<?php echo $this->baseUrl('/index/pagination/page/'.$this->next) ?>">next &raquo;</a>
<?php
	endif;
endif;
?>

Here's a list of all the parameters that become available in the navigation view: http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.usage.rendering.properties

Clone this wiki locally