In This Article

Usage

Paginating data collections

In order to paginate items into pages, Zend\Paginator must have a generic way
of accessing that data. For that reason, all data access takes place through
data source adapters. Several adapters ship with zend-paginator by default:

Adapter

Description

ArrayAdapter

Accepts a PHP array.

DbSelect

Accepts a Zend\Db\Sql\Select instance, plus either a Zend\Db\Adapter\Adapter or Zend\Db\Sql\Sql instance; paginates rows from a database.

Iterator

Accepts any Iterator instance.

NullFill

Dummy paginator.

Database optimizations

Instead of selecting every matching row of a given query, the DbSelect adapter
retrieves only the smallest amount of data necessary for displaying the
current page. Because of this, a second query is dynamically generated to
determine the total number of matching rows.

To create a paginator instance, you must supply an adapter to the constructor:

use Zend\Paginator\Adapter;
use Zend\Paginator\Paginator;
$paginator = new Paginator(new Adapter\ArrayAdapter($array));

In the case of the NullFill adapter, in lieu of a data collection you must
supply an item count to its constructor.

Although the instance is technically usable in this state, in your controller
action you'll need to tell the paginator what page number the user requested.
This allows advancing through the paginated data.

$paginator->setCurrentPageNumber($page);

The simplest way to keep track of this value is through a URL parameter. The
following is an example zend-router
route configuration:

The DbSelect adapter

Installation Requirements

The DbSelect adapter depends on the zend-db component, so be sure to have it
installed before getting started:

$ composer require zendframework/zend-db

Most adapters receive their datasets directly. However, the DbSelect adapter
requires a more detailed explanation regarding the retrieval and count of the
data from the database.

You do not have to retrieve data from the database prior to using the DbSelect
adapter; the adapter will do the retrieval for you, as well as provide a count
of total pages. If additional work has to be done on the database results which
cannot be expressed via the provided Zend\Db\Sql\Select, object you must
extend the adapter and override the getItems() method.

Additionally this adapter does not fetch all records from the database in
order to count them. Instead, the adapter manipulates the original query to
produce a corresponding COUNT query, and uses the new query to get the number
of rows. While this approach requires an extra round-trip to the database,
doing so is stillmany times faster than fetching an entire result set and using
count(), especially with large collections of data.

The database adapter will try and build the most efficient query that will
execute on pretty much any modern database. However, depending on your database
or even your own schema setup, there might be more efficient ways to get a
rowcount.

There are two approaches for doing this. The first is to extend the DbSelect
adapter and override the count() method:

This approach will probably not give you a huge performance gain on small
collections and/or simple select queries. However, with complex queries and
large collections, a similar approach could give you a significant performance
boost.

The DbSelect adapter also supports returning of fetched records using the
ResultSet subcomponent of zend-db.
You can override the concrete ResultSet implementation by passing an object
implementing Zend\Db\ResultSet\ResultSetInterface as the third constructor
argument to the DbSelect adapter:

Notice the view helper call near the end. PaginationControl accepts up to four
parameters: the paginator instance, a scrolling style, a view script name, and
an array of additional parameters.

The second and third parameters are very important. Whereas the view script name
is used to determine how the pagination control should look, the scrolling
style is used to control how it should behave. Say the view script is in the
style of a search pagination control, like the one below:

What happens when the user clicks the "next" link a few times? Well, any number of things could
happen:

The current page number could stay in the middle as you click through (as it
does on Yahoo!)

It could advance to the end of the page range and then appear again on the
left when the user clicks "next" one more time.

The page numbers might even expand and contract as the user advances (or
"scrolls") through them (as they do on Google).

There are four scrolling styles packaged with Zend Framework:

Scrolling style

Description

All

Returns every page. This is useful for dropdown menu pagination controls with relatively few pages. In these cases, you want all pages available to the user at once.

Elastic

A Google-like scrolling style that expands and contracts as a user scrolls through the pages.

Jumping

As users scroll through, the page number advances to the end of a given range, then starts again at the beginning of the new range.

Sliding

A Yahoo!-like scrolling style that positions the current page number in the center of the page range, or as close as possible. This is the default style.

The fourth and final parameter is reserved for an optional associative array of
variables that you want available in your view (available via $this). For
instance, these values could include extra URL parameters for pagination links.

By setting the default view script name, default scrolling style, and view
instance, you can eliminate the calls to PaginationControl completely:

use Zend\Paginator\Paginator;
use Zend\View\Helper\PaginationControl;
Paginator::setDefaultScrollingStyle('Sliding');
PaginationControl::setDefaultViewPartial('my_pagination_control');

When all of these values are set, you can render the pagination control inside
your view script by echoing the paginator instance:

<?= $this->paginator ?>

Using other template engines

Of course, it's possible to use zend-paginator with other template engines.
For example, with Smarty you might do the following:

$smarty-assign('pages', $paginator->getPages());

You could then access paginator values from a template like so:

{$pages.pageCount}

Example pagination controls

The following example pagination controls will help you get started with
zend-view: