UsagePaginating 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 Framework by default:
Adapters for Zend_PaginatorAdapterDescriptionArrayUse a PHP arrayDbSelect
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.
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:
For convenience, you may take advantage of the static
factory() method for the adapters packaged with Zend
Framework:
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.
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:
With the above route (and using Zend Framework MVC components),
you might set the current page number like this:
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:
view->paginator = $paginator;
]]>Rendering pages with view scripts
The view script is used to render the page items (if you're using
Zend_Paginator to do so) and display the pagination
control.
Because Zend_Paginator implements the SPL interface
IteratorAggregate,
looping over your items and displaying them is simple.