Zend Framework has an impressive library for pagination. Anyone can easily create pagination with some simple lines of code. Lets jumps to the tutorial to create pagination.
1. Model Code:
<?php class Users extends Zend_Db_Table { //set the current table name protected $_name = 'users'; //set the default primary key of that table protected $_primary = 'id'; /** * Create an SQL query for fetching * all the user information * * @param none * @return string -- sql query for * fetching user information * * @author M.M.H.Masud<masudiiuc@gmail.com> */ public function getAllUserInfo() { //create an instance of select method $condition = $this->select(); //select current table and fetch all info $condition -> from( $this->_name, '*' ); //set the where clause $condition -> where( 'status = ?', 'active'); //set order by clause $condition -> order( 'id' ); return $condition; } } ?>
This function makes a sql query using zend_db_table class inside a Zend Model Class. You can see the sql query with the following code:
2. Controller Code
Now use the following code inside the controller class to create the pagination with Zend_Paginator class.
<?php class PaginationController extends Zend_Controller_Action { /** * Class Variables */ var $selector; var $userTbl; /** * Set Default configuration for user table. * * @param none; * @return none; * @access public * * @author M.H.Masud<masudiiuc@gmail.com> */ public function init() { //create an instance of User Model $this->userTbl = new Users(); } /** * View the pagination * * @param none; * @return array -- Contains all the data and pagination settings * @access public * * @author M.H.Masud<masudiiuc@gmail.com> */ public function viewPaginationAction() { //get the sql query from the model $this->selector = $this->userTbl->getAllUserInfo(); //get current page number from url passed by Zend_Pagination Class $pageNumber = $this->_request->getParam('page', 1); //set no. of data for each page. $perPage = '10'; //create an instance of pagination class with the sql from model $paginator = Zend_Paginator::factory( $this->selector ); //set per page settings to Zend_Paginaton class $paginator->setItemCountPerPage( $perPage ); //set Current page number to Zend_Pagination class $paginator->setCurrentPageNumber( $pageNumber ); //render the view for pagination content. $this->view->paginator = $paginator; } } ?>
First of all get the current page number from the URL using the following line.
$pageNumber = $this->_request->getParam('page', 1);
Set the number of page contents using the following line:
$perPage = 10;
Create an instance of the paginator class with the sql query. Sql query is returned from the model class:
$selector = MODEL_NAME->getDataForPagination(); $paginator = Zend_Paginator::factory($selector);
Now set $pageNumber, $perPage to the $paginator object.
$paginator->setItemCountPerPage( $perPage ); $paginator->setCurrentPageNumber( $pageNumber );
Now pass this information to the view:
$this->view->paginator = $paginator;
3. View Code
Customizing the pagination view there are some method available. One of the best feature i used is developed by Anis(www.ajaxray.com). One of the finest programmer I seen in my short career. So I would like to show you the example first.
a. First create a common paginator file in scripts folder:
ex: pagination.phtml
place the following code in the file:
<?php $postfix = Zend_Registry::isRegistered('paginationData') ? Zend_Registry::get('paginationData') : '' ; if ($this->pageCount > 1): ?> <ul class="pagination"> <!-- Previous page link --> <li class="previous"> <a href="<?= $this->url(array('page' => $this->previous)) . $postfix ?>">«Previous</a> </li> <?php else: ?> <li class="previous-off">«Previous</li> <?php endif; ?> <!-- Numbered page links --> <?php foreach ($this->pagesInRange as $page): ?> <?php if ($page != $this->current): ?> <li><a href="<?= $this->url(array('page' => $page)) . $postfix ?>"><?= $page; ?></a></li> <?php else: ?> <li class="active"><?= $page; ?></li> <?php endif; ?> <?php endforeach; ?> <li class="next"><a href="<?= $this->url(array('page' => $this->next)) . $postfix ?>">Next »</a></li> <?php else: ?> <li class="next-off">Next »</li> <?php endif; ?> </ul> <?php endif; ?>
Then called this script in your Action phtml script file. Use the following code to do this:
<?=$this->paginationControl($this->paginator,'Sliding', ‘pagination.phtml'); ?>
This code will call the pagination.phtml file and then render the view and place the pagination with the formating in your layout.
NB: Create the pagination CSS by yourself as you want.
Another technique is given in
http://framework.zend.com/wiki/display/ZFPROP/Zend_Paginator+-+Matthew+Ratzloff
Please check this for manual and drop down based pagination.
Special Thanks goes to:
Anis - http://www.ajaxray.com
Reference
http://www.zfforums.com/
http://framework.zend.com/
good example.
not bad.. :<<