How to use pagination in Zend Framework 2.5

Zend\Paginator is an useful component which allows to add a handy pagination controller in our Zend Framework projects. In my last tutorial I have explained how to use the models in Zend Framework 2.5 and in this tutorial I will explain how to add pagination in a Zend Framework pagination.

To add pagination in Zend Framework, first I will add in my models this namespaces:

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

Then, I will add a new function in my model that will extract the data from database based on page number:


public function fetchAll($paginated=false)
{
if ($paginated) {
// create a new Select object for the table album
$select = new Select('album');
// create a new result set based on the Album entity
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
// create a new pagination adapter object
$paginatorAdapter = new DbSelect(
// our configured select object
$select,
// the adapter to run it against
$this->tableGateway->getAdapter(),
// the result set to hydrate
$resultSetPrototype
);
$paginator = new Paginator($paginatorAdapter);
return $paginator;
}
$resultSet = $this->tableGateway->select();
return $resultSet;
}

In my Controller I will use this code in my index action:

$paginator = $this->getAlbumTable()->fetchAll(true);
// set the current page to what has been passed in query string, or to 1 if none set
$paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
// set the number of items per page to 10
$paginator->setItemCountPerPage(10);

return new ViewModel(array(
'paginator' => $paginator
));

to get the number of the page, set item per count and get the data from my model.

In my index.phtml file I will use this code:
<table class="table">
<tr>
<th>Title</th>
<th>Artist</th>
<th>&nbsp;</th>
</tr>
<?php foreach ($this->paginator as $album) : // <-- change here! ?>
<tr>
<td><?php echo $this->escapeHtml($album->title);?></td>
<td><?php echo $this->escapeHtml($album->artist);?></td>
<td>
<a href="<?php echo $this->url('album',
array('action' => 'edit', 'id' => $album->id));?>">Edit</a>
<a href="<?php echo $this->url('album',
array('action' => 'delete', 'id' => $album->id));?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>

to display the items from database.

Then, I will create a folder called partial in the folder module\Newapp\view. Also I will create there the file paginator.phtml to display the pagination numbers.

The file paginator.phtml will contains this code:

<?php if ($this->pageCount): ?>
<div>
<ul class="pagination">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->previous; ?>">
<<
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
<<
</a>
</li>
<?php endif; ?>

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li>
<a href="<?php echo $this->url($this->route);?>?page=<?php echo $page; ?>">
<?php echo $page; ?>
</a>
</li>
<?php else: ?>
<li class="active">
<a href="#"><?php echo $page; ?></a>
</li>
<?php endif; ?>
<?php endforeach; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->next; ?>">
>>
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
>>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>

To call the file paginator.phtml from my main index.phtml file, I will use this code in my index.phtml file:

<?php
// add at the end of the file after the table
echo $this->paginationControl(
// the paginator object
$this->paginator,
// the scrolling style
'sliding',
// the partial to use to render the control
'partial/paginator.phtml',
// the route to link to when a user clicks a control link
array(
'route' => 'album'
)
);
?>

I have also created a video where you can see how the pagination works in Zend Framework:









Leave a Comment