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.

a01

Create into it, trees table named.

a02

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

a03

Into this table insert about 27 rows.

a17

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

zoo00

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

43

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

a05

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

a06

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

zoo04

In Projects window You see tree structure of Your application.

a07

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.

a08

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.

a09

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

a10

In Filter write enable layout and click on Run button.

a11

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

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

a12

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.

a13

Click on Run button.

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

a14

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.

a15

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:

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

a16

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

a18

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;
}

a19

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>
<p>
<?php foreach ($this->trees as $tree): ?>
<?php echo $tree['name'] ?><br/>
<?php endforeach; ?>
</p>
<br/>
<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>
<br/>

a20

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:

a21

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

a22

 

Clicking again on next link You see another items.

a23

 

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

a24

And click on previous link.

a25

Zend_Paginator class is very usefull.