Our blog

 

Zend Paginator – Actually Pretty Painless :)

With some trepidation I decided that I really had to implement pagination into a Zend Framework project I am working on. Zend Framework is great, but some of the sections can be a little tricky to get your head around at first attempt.

However, less than an hour after first looking at it, I have now got my system spitting out results in a nicely paginated ten at a time. The documentation on the Zend Framework reference is a little sparse but suffice to say that if you are using Zend_Db then actually its pretty easy.

The only bit that is a little tricky is getting the actual page controls to display, as you are left to code your own (though they do supply some code).

One major irritation with the Zend Framework reference guide is that I seem to be unable to copy and paste the code examples, it ends up looking like this:

PHP:
  1. <!--
  2.    2.
  3.       See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
  4.    3.
  5.       -->
  6.    4.
  7.        
  8.    5.
  9.       <?php if ($this->pageCount): ?>
  10.    6.
  11.       <div class="paginationControl">
  12.    7.
  13.       <!-- Previous page link -->
  14.    8.
  15.       <?php if (isset($this->previous)): ?>
  16.    9.
  17.         <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  18.   10.
  19.           &lt;Previous
  20.   11.
  21.         </a> |
  22.   12.

note the line numbers, great.

this tutorial was really useful though:

Here is some code snippets:

Controller

PHP:
  1. public function viewAction(){       
  2.         $this->view->input = $input = $this->_request->getPost('search');
  3.         $paginator = $this->_model->tableSearchAllPaginator($input);
  4.         $paginator->setCurrentPageNumber($this->_getParam('page'));
  5.         $this->view->paginator = $paginator;
  6.     }

Model

PHP:
  1. public function tableSearchAllPaginator($input, $fields_to_select=null){
  2.         $table = $this->getTable();
  3.         $select = $table->select();
  4.         $fields = $table->info(Zend_Db_Table_Abstract::COLS);
  5.         if(!empty($input)) {
  6.             foreach($fields as $field) {
  7.                 $select->orWhere("$field like ?", "%$input%");
  8.             }
  9.         }
  10.         if(!empty($fields_to_select)) {
  11.             $select->columns($fields_to_select);
  12.         }
  13.         $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
  14.         return $paginator;
  15.     }

View Script

PHP:
  1. echo $this->partial('_viewtable_page.phtml', array('paginator'=>$this->paginator, 'controller'=>'products'));

View Table Page Partial

PHP:
  1. if (count($this->paginator)) {
  2.     echo '<h4>Found ' . count($this->paginator) . ' Results</h4>';
  3.     echo '<table class="grid">';
  4.     foreach($this->paginator as $k=>$row) {
  5.         //EC_Debug::dump($k);
  6.         $row = $row->toArray();
  7.         //EC_Debug::diedump($row);
  8.         if($k == 0) {
  9.             echo '<tr>';
  10.             foreach($row as $f=>$v) {
  11.                 echo "<th>$f</th>";
  12.             }
  13.             echo '</tr>';
  14.         }
  15.         echo '<tr>';
  16.         foreach($row as $f=>$v) {
  17.             $extra = '';
  18.             if($f=='id') {
  19.                 $extra = '<a href="' . $this->url(array('controller'=>$this->controller, 'action'=>'edit', 'id'=>$v), null, true) . '"><img src="' . $this->baseUrl() . '/style/icons/application_edit.png" border="0" alt="edit"></a>';
  20.                 $extra .= ' <a href="' . $this->url(array('controller'=>$this->controller, 'action'=>'delete', 'id'=>$v), null, true) . '"><img src="' . $this->baseUrl() . '/style/icons/cancel.png" border="0" alt="delete"></a>';
  21.             }
  22.             echo "<td>$extra $v</td>";
  23.         }
  24.         echo '</tr>';
  25.     }
  26.     echo '</table>';
  27.     echo $this->paginationControl($this->paginator, 'Sliding', '_paginator.php');
  28. }else{
  29.     echo 'no results...';
  30. }

Pagination Controls Partial

PHP:
  1. <?php if ($this->pageCount): ?>
  2. <div class="pagination">
  3.  
  4. <?php if (isset($this->previous)): ?>
  5.   <a href="<?= $this->url(array('page' => $this->previous)); ?>">&laquo; PREV</a> -
  6. <?php endif; ?>
  7.  
  8. <?php
  9.     /* Page links */
  10.  
  11.     foreach ($this->pagesInRange as $page): ?>
  12.     <a href="<?= $this->url(array('page' => $page)); ?>" <?php if($page == $this->current): ?>id="selected"><?php endif; ?><?= $page; ?></a>
  13. <?php endforeach; ?>
  14.  
  15. <?php if (isset($this->next)): ?>
  16.  - <a href="<?= $this->url(array('page' => $this->next)); ?>">Next&gt;</a>
  17. <?php endif; ?>
  18.  
  19. </div>
  20. <?php endif; ?>

More Reading:

 

Leave a Reply