Example for pagination using Zend_Paginator

You may interesting to view Zend_Paginator Full Example.

Here is simple and easy understand example to show you how to use Zend_Paginator.

After you get search results from database, pass results to Zend_Paginator.
Add following code to your controller page.

/*
* Get the page number , default 1
*/
$page = $this->_getParam('page',1);
/*
* Object of Zend_Paginator
*/
$paginator = Zend_Paginator::factory($result);
/*
* Set the number of counts in a page
*/
$paginator->setItemCountPerPage(2);
/*
* Set the current page number
*/
$paginator->setCurrentPageNumber($page);
/*
* Assign to view
*/
$this->view->paginator = $paginator;

Code example in view script:

<h1>Example</h1>
<?php if (count($this->paginator)): ?>
<ul>
<?php
/*
* Iterate through the paginator to show the results
*/
foreach ($this->paginator as $item): ?>
<li><?php echo $item['FirstName'] ." ".$item['LastName']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php
/*
* Print the pagination of type , drop down or search type or of your choice.
*/
echo $this->paginationControl($this->paginator,
                                    'Sliding',
                                    '/my_pagination_control.phtml'); ?>

Create a paginator template in application/views/scripts/my_pagination_control.phtml . I have added the 3 types of pagination in following code. The defalut use one is drop down. You can simply comment it and remove other’s comment to use.

Sameple of paginator template for Zend_Paginator

<?php
/*
* Search pagination
*/
?>
<?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)); ?>"> << 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; */?>
<?php
/*
 * Item pagination example
 *
 */
?>
<!--
See <a href="http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination" title="http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination">http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination</a>
-->
<?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; */?>
<?php
/*
 * Drop down paginaton example
 */
?>
<?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>

Zend Paginator Example 1

Zend Paginator Example 1

Zend Paginator Example 2

Zend Paginator Example 2

Zend Paginator Example 3

Zend Paginator Example 3

affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

One Response to “Example for pagination using Zend_Paginator”

  • Janna Turli says:

    It is actually a really helpful read for me, Need to admit you may be 1 in the a lot effective bloggers I ever saw.Many thanks for putting up it educational article.

  • Leave a Reply:

    Name (required):
    Mail (will not be published) (required):
    Website:
    Comment (required):
    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>