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 Framework by default:
Table 124. Adapters for Zend_Paginator
| Adapter | Description |
|---|---|
| Array | Use a PHP array |
| DbSelect |
Use a Zend_Db_Select
instance, which will return an array
|
| DbTableSelect |
Use a Zend_Db_Table_Select
instance, which will return an instance of
Zend_Db_Table_Rowset_Abstract.
This provides additional information about the
result set, such as column names.
|
| Iterator |
Use an Iterator
instance
|
| Null |
Do not use Zend_Paginator to manage
data pagination. You can still take advantage of
the pagination control feature.
|
Note
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.
See the setRowCount() method in the DbSelect
adapter for more information.
To create an instance of Zend_Paginator, you must
supply an adapter to the constructor:
<?php
$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
Framework:
<?php
$paginator = Zend_Paginator::factory($array);
Note
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.
<?php
$paginator->setCurrentPageNumber($page);
The simplest way to keep track of this value is through a URL.
Although we recommend using a
Zend_Controller_Router_Interface-compatible
router to handle this, it is not a requirement.
The following is an example route you might use in an INI configuration file:
<?php
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:
<?php
$paginator->setCurrentPageNumber($this->_getParam('page'));
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:
<?php
$this->view->paginator = $paginator;




