Pagination in Zend Framework 1.12

One day, when I was creating application in Zend I came accross problem with display a lot of records from database. All these rows was in the same site, but I would like to display these in partial of data. I searched all Zend documentation and … I find Zend_Pagination class , which divides data on partials. Owing to the fact, we may have less data on one page and linking to another pages with other data.

Beginn with creating project which displasy us trees from databases page by page.

First create in phpMyAdmin trees database named.


Create into it, trees table named.


In this table set two columns: id as primary key with autoincrement and name column as varchar(100).


Into this table insert about 27 rows.


Open Netbeans and choose from menu File->New Project.


In New Project window choose PHP from Categories section and PHP Application from Projects and click on Next button.


In Project Name field write Trees and click on Next button.


In Project URL field add on the end of path public word and click on Next button.


From list of framework select Zend PHP Web Framework and click on Finish button.


In Projects window You see tree structure of Your application.


Open application.ini from application/configs node and configure database. Paste this code into production section:

resources.db.adapter = "pdo_mysql"
resources.db.params.dbname = "trees" = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "dorota"
resources.db.params.charset = "utf8"

This code connect to Your database, change your user and password to database.


In .htaccess file in public node set on top this code:

SetEnv APPLICATION_ENV development
DirectoryIndex index.php

This allow to view errors on the website if it appear.


And set layout for project. Right click on project node and select Zend->Run Command.


In Filter write enable layout and click on Run button.


From layouts/scripts node open layout.phtml file and paste into it code:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php echo $this->layout()->content; ?>


In this time You may create Trees class reference to table in database of trees name. So open Zend Run Command window and write into Filter field create db-table and in Parameters field Trees trees.


Click on Run button.

In models/DbTable node You see Trees.php file with Application_Model_DbTable_Trees class.


Go to controller. Open IndexController.php file from application/controllers node. Paste into indexAction method this code:

$trees = new Application_Model_DbTable_Trees();
$this->view->trees = $trees->fetchAll();

First You call Trees class creating its object and for index view pass all records from this object. Method fetchAll getting all rows from database from trees table.


This records is giving to view of controller as array with trees name. So go to view index. Open in views/scripts/index index.phtml file and paste in it code:

<?php foreach ($this->trees as $tree): ?>
<?php echo $tree['name'] ?><br/>
<?php endforeach; ?>


You see list of all trees on one page. Run this application and see it:


Left us to add paggination. So go to index controller and change its indexAction method. Paste into indexAction code:

$trees = new Application_Model_DbTable_Trees();
$paginator = Zend_Paginator::factory($trees->fetchAll());
$allItems = $paginator->getTotalItemCount();
$countPages = $paginator->count();

$p = $this->getRequest()->getParam('p');
} else $paginator->setCurrentPageNumber(1);

$currentPage = $paginator->getCurrentPageNumber();

$this->view->trees = $paginator;
$this->view->countItems = $allItems;
$this->view->countPages = $countPages;
$this->view->currentPage = $currentPage;

if($currentPage == $countPages)
     $this->view->nextPage = $countPages;
     $this->view->previousPage = $currentPage-1;
else if($currentPage == 1)
     $this->view->nextPage = $currentPage+1;
     $this->view->previousPage = 1;
else {
    $this->view->nextPage = $currentPage+1;
    $this->view->previousPage = $currentPage-1;


All records from database You may send as parameter to factory method of Zend_Paginator object. This class contains a lot of usefull method. You may set current page, get current page and count of pages, set count items per page and so on. Some of them You send to the view.

You may change either index.phtml view file. Paste its code for it:

<h2>Trees <?php echo "( ".$this->countItems." )"; ?></h2>
<?php foreach ($this->trees as $tree): ?>
<?php echo $tree['name'] ?><br/>
<?php endforeach; ?>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'index','p'=>$this->previousPage)) ?>"> previous </a>
<?php echo "".$this->currentPage."/".$this->countPages; ?>
<a href="<?php echo $this->url(array('controller'=>'index','action'=>'index','p'=>$this->nextPage)) ?>"> next </a>


In view You must in url of links send parameter with current page. It help controller navigate to proper page with partial data.

Run your application:


Click on next link. You see other 8 items from database.



Clicking again on next link You see another items.



And You may last time click on next link, becouse it is last page.


And click on previous link.


Zend_Paginator class is very usefull.