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"
resources.db.params.host = "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:



Trees





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:

Trees







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());
$paginator->setDefaultItemCountPerPage(8);
$allItems = $paginator->getTotalItemCount();
$countPages = $paginator->count();

$p = $this->getRequest()->getParam('p');
if(isset($p))
{
  $paginator->setCurrentPageNumber($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:

Trees