PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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.


<html>
<body>
<h1>Example</h1>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
  <li><?php echo $item?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

<?php echo $this->paginationControl($this->paginator,
                                    
'Sliding',
                                    
'my_pagination_control.phtml'); ?>
</body>
</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:

Table 129. 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:

<?php
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:


<?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:

<?php
$smarty
->assign('pages'$paginator->getPages());

You could then access paginator values from a template like so:

<?php
{$pages->pageCount}

Example pagination controls

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

Search pagination:


<!--
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
-->

<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    &lt; Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
  <?php if ($page != $this->current): ?>
    <a href="<?php echo $this->url(array('page' => $page)); ?>">
        <?php echo $page?>
    </a> |
  <?php else: ?>
    <?php echo $page?> |
  <?php endif; ?>
<?php 
endforeach; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a>
<?php else: ?>
  <span class="disabled">Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>

Item pagination:


<!--
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
-->

<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?php echo $this->firstItemNumber?> - <?php echo $this->lastItemNumber?>
of <?php echo $this->totalItemCount?>

<!-- First page link -->
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
    First
  </a> |
<?php else: ?>
  <span class="disabled">First</span> |
<?php endif; ?>

<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
  <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
    &lt; Previous
  </a> |
<?php else: ?>
  <span class="disabled">&lt; Previous</span> |
<?php endif; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
    Next &gt;
  </a> |
<?php else: ?>
  <span class="disabled">Next &gt;</span> |
<?php endif; ?>

<!-- Last page link -->
<?php if (isset($this->next)): ?>
  <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
    Last
  </a>
<?php else: ?>
  <span class="disabled">Last</span>
<?php endif; ?>

</div>
<?php endif; ?>

Dropdown pagination:


<?php if ($this->pageCount): ?>
<select id="paginationControl" size="1">
<?php foreach ($this->pagesInRange as $page): ?>
  <?php $selected = ($page == $this->current) ? ' selected="selected"' ''?>
  <option value="<?php
        
echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
    <?php echo $page?>
  </option>
<?php endforeach; ?>
</select>
<?php endif; ?>

<script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
</script>
<script type="text/javascript">
$('paginationControl').observe('change', function() {
    window.location = this.options[this.selectedIndex].value;
})
</script>

Listing of properties

The following options are available to pagination control view partials:

Table 130. 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

Zend Framework