Introduction | Zend_Paginator Programmer's Reference Guide | Configuration

# 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 Framework by default:

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:

1. $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:

1. $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.

1. $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:

1. routes.example.route = articles/:articleName/:page
2. routes.example.defaults.controller = articles
3. routes.example.defaults.action = view
4. routes.example.defaults.page = 1
5. routes.example.reqs.articleName = \w+
6. routes.example.reqs.page = \d+

With the above route (and using Zend Framework MVC components), you might set the current page number like this:

1. $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:

1. $this->view->paginator = $paginator;

# The DbSelect and DbTableSelect adapter

The usage of most adapters is pretty straight-forward. However, the database adapters require a more detailed explanation regarding the retrieval and count of the data from the database.

To use the DbSelect and DbTableSelect adapters you don't have to retrieve the data upfront from the database. Both adapters do the retrieval for you, aswell as the counting of the total pages. If additional work has to be done on the database results the adapter getItems() method has to be extended in your application.

Additionally these adapters do not fetch all records from the database in order to count them. Instead, the adapters manipulates the original query to produce the corresponding COUNT query. Paginator then executes that COUNT query to get the number of rows. This does require an extra round-trip to the database, but this is many times faster than fetching an entire result set and using count(). Especially with large collections of data.

The database adapters will try and build the most efficient query that will execute on pretty much all modern databases. However, depending on your database or even your own schema setup, there might be more efficient ways to get a rowcount. For this scenario the database adapters allow you to set a custom COUNT query. For example, if you keep track of the count of blog posts in a separate table, you could achieve a faster count query with the following setup:

1. $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
2. $adapter->setRowCount(
3. $db->select()
4. ->from(
5. 'item_counts',
6. array(
7. Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count'
8. )
9. )
10. );
12. $paginator = new Zend_Paginator($adapter);

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.

# 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.

1. <html>
2. <body>
3. <h1>Example</h1>
4. <?php if (count($this->paginator)): ?>
5. <ul>
6. <?php foreach ($this->paginator as $item): ?>
7. <li><?php echo $item; ?></li>
8. <?php endforeach; ?>
9. </ul>
10. <?php endif; ?>
12. <?php echo $this->paginationControl($this->paginator,
13. 'Sliding',
14. 'my_pagination_control.phtml'); ?>
15. </body>
16. </html>

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

The second and third 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:

Scrolling styles for Zend_Paginator
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 additional variables that you want available in your view partial (available via $this). For instance, these values could include extra URL parameters for pagination links.

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

1. Zend_Paginator::setDefaultScrollingStyle('Sliding');
2. Zend_View_Helper_PaginationControl::setDefaultViewPartial(
3. 'my_pagination_control.phtml'
4. );
5. $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:

1. <?php echo $this->paginator; ?>

> Note: Of course, it's possible to use Zend_Paginator with other template engines. For example, with Smarty you might do the following:

> 1. $smarty->assign('pages', $paginator->getPages());
> You could then access paginator values from a template like so:
> 1. {$pages->pageCount}

# Example pagination controls

The following example pagination controls will hopefully help you get started:

Search pagination:

1. <!--
2. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
3. -->
5. <?php if ($this->pageCount): ?>
6. <div class="paginationControl">
7. <!-- Previous page link -->
8. <?php if (isset($this->previous)): ?>
9. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
10. &lt; Previous
11. </a> |
12. <?php else: ?>
13. <span class="disabled">&lt; Previous</span> |
14. <?php endif; ?>
16. <!-- Numbered page links -->
17. <?php foreach ($this->pagesInRange as $page): ?>
18. <?php if ($page != $this->current): ?>
19. <a href="<?php echo $this->url(array('page' => $page)); ?>">
20. <?php echo $page; ?>
21. </a> |
22. <?php else: ?>
23. <?php echo $page; ?> |
24. <?php endif; ?>
25. <?php endforeach; ?>
27. <!-- Next page link -->
28. <?php if (isset($this->next)): ?>
29. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
30. Next &gt;
31. </a>
32. <?php else: ?>
33. <span class="disabled">Next &gt;</span>
34. <?php endif; ?>
35. </div>
36. <?php endif; ?>

Item pagination:

1. <!--
2. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
3. -->
5. <?php if ($this->pageCount): ?>
6. <div class="paginationControl">
7. <?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
8. of <?php echo $this->totalItemCount; ?>
10. <!-- First page link -->
11. <?php if (isset($this->previous)): ?>
12. <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
13. First
14. </a> |
15. <?php else: ?>
16. <span class="disabled">First</span> |
17. <?php endif; ?>
19. <!-- Previous page link -->
20. <?php if (isset($this->previous)): ?>
21. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
22. &lt; Previous
23. </a> |
24. <?php else: ?>
25. <span class="disabled">&lt; Previous</span> |
26. <?php endif; ?>
28. <!-- Next page link -->
29. <?php if (isset($this->next)): ?>
30. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
31. Next &gt;
32. </a> |
33. <?php else: ?>
34. <span class="disabled">Next &gt;</span> |
35. <?php endif; ?>
37. <!-- Last page link -->
38. <?php if (isset($this->next)): ?>
39. <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
40. Last
41. </a>
42. <?php else: ?>
43. <span class="disabled">Last</span>
44. <?php endif; ?>
46. </div>
47. <?php endif; ?>

Dropdown pagination:

1. <?php if ($this->pageCount): ?>
2. <select id="paginationControl" size="1">
3. <?php foreach ($this->pagesInRange as $page): ?>
4. <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
5. <option value="<?php
6. echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
7. <?php echo $page; ?>
8. </option>
9. <?php endforeach; ?>
10. </select>
11. <?php endif; ?>
13. <script type="text/javascript"
14. src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
15. </script>
16. <script type="text/javascript">
17. $('paginationControl').observe('change', function() {
18. window.location = this.options[this.selectedIndex].value;
19. })
20. </script>

# Listing of properties

The following options are available to pagination control view partials:

Properties available to view partials
Property | Type | Description
--- | --- | ---
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
itemCountPerPage | integer | Maximum number of items available to each 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

Introduction | Zend_Paginator Programmer's Reference Guide | Configuration