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:
|Array||Use a PHP array|
Use an »
Do not use
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);
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, you'll need to tell the paginator what page number the user requested in order to allow 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/:pageNumber routes.example.defaults.controller = articles routes.example.defaults.action = view routes.example.defaults.pageNumber = 1 routes.example.reqs.articleName = \w+ routes.example.reqs.pageNumber = \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;
The view script is used to render the page items (if you're using
Zend_Paginator to do so) and display the pagination
Zend_Paginator implements the SPL interface
looping over your items and displaying them is simple.
Notice the view helper call near the end. PaginationControl takes the paginator instance, an optional scrolling style, and an optional view partial.
Despite being optional, the latter two parameters are very important. Whereas the view partial is used to determine how the pagination control should look, the scrolling style is used to control how it should behave. Say the view partial 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!), or 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:
|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.|
By setting the default view partial, default scrolling style, and view instance, you can eliminate the calls to PaginationControl completely:
Zend_Paginator::setDefaultScrollingStyle('Sliding'); Zend_View_Helper_PaginationControl::setDefaultViewPartial('my_pagination_control.phtml'); $paginator->setView($view);
When all of these values are set, you can render the pagination control inside your view script with a simple echo statement:
= $this->paginator; ?>
The following example pagination controls will hopefully help you get started:
The following options are available to pagination control view partials:
|first||integer||First page number (i.e., 1)|
|firstItemNumber||integer||Absolute number of the first item on this page|
|firstPageInRange||integer||First page in the range returned by the scrolling style|
|current||integer||Current page number|
|currentItemCount||integer||Number of items on this page|
|last||integer||Last page number|
|lastItemNumber||integer||Absolute number of the last item on this page|
|lastPageInRange||integer||Last page in the range returned by the scrolling style|
|next||integer||Next page number|
|pageCount||integer||Number of pages|
|pagesInRange||array||Array of pages returned by the scrolling style|
|previous||integer||Previous page number|
|totalItemCount||integer||Total number of items|