In order to paginate items into pages,
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 Framework by default:
Table 124. Adapters for Zend_Paginator
|Array||Use a PHP array|
Do not use
Instead of selecting every matching row of a given query, the DbSelect and DbTableSelect adapters retrieve 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. However, it is
possible to directly supply a count or count query yourself.
setRowCount() method in the DbSelect
adapter for more information.
To create an instance of
Zend_Paginator, you must
supply an adapter to the constructor:
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));
For convenience, you may take advantage of the static
factory() method for the adapters packaged with Zend
$paginator = Zend_Paginator::factory($array);
In the case of the
Null 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 him to advance through the paginated data.
The simplest way to keep track of this value is through a URL.
Although we recommend using a
router to handle this, it is not a requirement.
The following is an example route you might use in an INI configuration file:
routes.example.route = articles/:articleName/:page
routes.example.defaults.controller = articles
routes.example.defaults.action = view
routes.example.defaults.page = 1
routes.example.reqs.articleName = \w+
routes.example.reqs.page = \d+
With the above route (and using Zend Framework MVC components), you might set the current page number like this:
There are other options available; see Configuration for more on them.
Finally, you'll need to assign the paginator instance to your view.
If you're using
Zend_View with the ViewRenderer action
helper, the following will work:
$this->view->paginator = $paginator;