PHP programmer of bangladesh
A PHP Programmer of Bangladesh

Zend Framework: How to create Pagination using Zend Paginator class

October 2nd 2008 in Code Library, Help, Other Blogs, PHP, Programming

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:

  1.  
  2. <?php
  3. class Users extends Zend_Db_Table
  4. {
  5. //set the current table name
  6. protected $_name = 'users';
  7.  
  8. //set the default primary key of that table
  9. protected $_primary = 'id';
  10.  
  11. /**
  12. * Create an SQL query for fetching
  13. * all the user information
  14. *
  15. * @param none
  16. * @return string -- sql query for
  17. * fetching user information
  18. *
  19. * @author M.M.H.Masud<masudiiuc@gmail.com>
  20. */
  21. public function getAllUserInfo()
  22. {
  23. //create an instance of select method
  24. $condition = $this->select();
  25.  
  26. //select current table and fetch all info
  27. $condition -> from( $this->_name, '*' );
  28.  
  29. //set the where clause
  30. $condition -> where( 'status = ?', 'active');
  31.  
  32. //set order by clause
  33. $condition -> order( 'id' );
  34.  
  35. return $condition;
  36. }
  37. }
  38. ?>
  39.  

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:

  1.  
  2. echo $condition->__toString();
  3.  

2. Controller Code

Now use the following code inside the controller class to create the pagination with Zend_Paginator class.

  1.  
  2. <?php
  3.  
  4. class PaginationController extends Zend_Controller_Action
  5. {
  6. /**
  7. * Class Variables
  8. */
  9. var $selector;
  10. var $userTbl;
  11.  
  12. /**
  13. * Set Default configuration for user table.
  14. *
  15. * @param none;
  16. * @return none;
  17. * @access public
  18. *
  19. * @author M.H.Masud<masudiiuc@gmail.com>
  20. */
  21. public function init()
  22. {
  23. //create an instance of User Model
  24. $this->userTbl = new Users();
  25. }
  26.  
  27. /**
  28. * View the pagination
  29. *
  30. * @param none;
  31. * @return array -- Contains all the data
  32. and pagination settings
  33. * @access public
  34. *
  35. * @author M.H.Masud<masudiiuc@gmail.com>
  36. */
  37. public function viewPaginationAction()
  38. {
  39. //get the sql query from the model
  40. $this->selector = $this->userTbl->getAllUserInfo();
  41.  
  42. //get current page number from url passed by Zend_Pagination Class
  43. $pageNumber = $this->_request->getParam('page', 1);
  44.  
  45. //set no. of data for each page.
  46. $perPage = '10';
  47.  
  48. //create an instance of pagination class with the sql from model
  49. $paginator = Zend_Paginator::factory( $this->selector );
  50.  
  51. //set per page settings to Zend_Paginaton class
  52. $paginator->setItemCountPerPage( $perPage );
  53.  
  54. //set Current page number to Zend_Pagination class
  55. $paginator->setCurrentPageNumber( $pageNumber );
  56.  
  57. //render the view for pagination content.
  58. $this->view->paginator = $paginator;
  59. }
  60. }
  61. ?>
  62.  

First of all get the current page number from the URL using the following line.

  1.  
  2. $pageNumber = $this->_request->getParam('page', 1);
  3.  

Set the number of page contents using the following line:

  1.  
  2. $perPage = 10;
  3.  

Create an instance of the paginator class with the sql query. Sql query is returned from the model class:

  1.  
  2. $selector = MODEL_NAME->getDataForPagination();
  3. $paginator = Zend_Paginator::factory($selector);
  4.  

Now set $pageNumber, $perPage to the $paginator object.

  1.  
  2. $paginator->setItemCountPerPage( $perPage );
  3. $paginator->setCurrentPageNumber( $pageNumber );
  4.  

Now pass this information to the view:

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

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:

  1.  
  2. <?php
  3. $postfix = Zend_Registry::isRegistered('paginationData')
  4. ? Zend_Registry::get('paginationData') : '' ;
  5.  
  6. if ($this->pageCount > 1):
  7. ?>
  8. <ul class="pagination">
  9. <!-- Previous page link -->
  10. <?php if (isset($this->previous)): ?>
  11. <li class="previous">
  12. <a href="<?= $this->url(array('page' => $this->previous)) . $postfix ?>">«Previous</a>
  13. </li>
  14.  
  15. <?php else: ?>
  16. <li class="previous-off">«Previous</li>
  17.  
  18. <?php endif; ?>
  19.  
  20. <!-- Numbered page links -->
  21. <?php foreach ($this->pagesInRange as $page): ?>
  22. <?php if ($page != $this->current): ?>
  23. <li><a href="<?= $this->url(array('page' => $page)) . $postfix ?>"><?= $page; ?></a></li>
  24.  
  25. <?php else: ?>
  26. <li class="active"><?= $page; ?></li>
  27.  
  28. <?php endif; ?>
  29. <?php endforeach; ?>
  30.  
  31. <!-- Next page link -->
  32. <?php if (isset($this->next)): ?>
  33. <li class="next"><a href="<?= $this->url(array('page' => $this->next)) . $postfix ?>">Next »</a></li>
  34.  
  35. <?php else: ?>
  36. <li class="next-off">Next »</li>
  37.  
  38. <?php endif; ?>
  39. </ul>
  40.  
  41. <?php endif; ?>
  42.  

Then called this script in your Action phtml script file. Use the following code to do this:

  1.  
  2. <?=$this->paginationControl($this->paginator,'Sliding', ‘pagination.phtml'); ?>
  3.  

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/


Viewing 1 Comment

Trackbacks

close Reblog this comment
blog comments powered by Disqus

Google Chrome: Another gift of Google.

Browsers are most essential part of modern internet technology. Lots of browsers are available in current web world. Internet Explorer by Microsoft, Mozila Firefox by Linux, Safari by Mac etc. Among this Mozila firefox is most favorite to the web developers for its features.

Google Chrome is a new browser which is running under beta [...]

Google Chrome: Another gift of Google.Previous Entry

Where i am ???



Currently I am working in Right Brain Solution(RBS) as a Software Engineer(Team Leader). I start my journey in RBS from 6th Nov, 2007.
Where i Was???
I am a php professional from Bangladesh. I start my journey as a professional from 1st july, 2006 in EVOKNOW Incorporation. I was there for one and half year. My Position was there "Software Engineer(Lead Developer)". I left EVOKNOW at 5th Nov, 2007.
Self Formed
I do some freelancing work with some of my friends under this name. Our dream is to make it world famous in the field of web development & solution. If you would like to work with us, send us your proposal to technopeoplebd@gmail.com
Social Networks
View Md. Mahabubul Hasan Masud's profile on LinkedIn
Moonrise: What i feeling???
Feelings from moonrise
More items See these feelings on moonrise