πŸ“œ ⬆️ ⬇️

Making pagination in the Zend Framework

When I was very small, I didn’t know what a Zend_paginator is, and indeed what a Zend is.
Creating page-by-page navigation was not so much a problem for me, but an occupation, at least, routine and nasty. However, when studying ZF, I discovered a wonderful thing. So let's understand a little bit.

We take the article that was written earlier as a basis, and on the basis of it we will try to make a guest book with page navigation.

Let's start with the database:
for simplicity, we take only 3 fields id, name, message
it turns out such a dump:
CREATE TABLE `guestbook` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(16) default NULL,
`message` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=1 ;

directory structure
We have such a structure.
From the new here, only my_pagination.phtml appeared - the helper file, which contains the look of our β€œnavigator”, but a little more about it.

The file Guestbook.php contains an indication of the table with which we will work.
/application/models/Guestbook.php
<?php
class Guestbook extends Zend_Db_Table
{
protected $_name = 'guestbook';
}

')
The IndexController file contains 3 methods: indexAction, addAction and init.
/application/controllers/IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
function indexAction(){
$gb = new Guestbook();
$num = 10;
$page = $this->_getParam('page');
if($page<1 or empty($page)){$page = 1;}
$select = $gb->select()->from('guestbook',array('count'=>'COUNT(*)'))->order('id DESC ');
$total = $this->view->table = $gb->fetchRow($select)->toArray();
$start = $page*$num-$num;
$select = $gb->select()->from('guestbook')->order('id DESC ')->limit($start,$num);
$result = $this->view->table = $gb->fetchAll($select)->toArray();
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_array($result));
$paginator->setCurrentPageNumber($this->_getParam($page));
$paginator->setItemCountPerPage($num);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('/my_pagination.phtml');
$this->view->paginator = $paginator;
}
function addAction(){
if($this->_request->isPost()){
$filter = new Zend_Filter_StripTags();
$name = trim($filter->filter($this->_request->getPost('name')));
$message = trim($filter->filter($this->_request->getPost('message')));
if(!empty($name) && !empty($message)){
$data = array(
'name' => $name,
'message' => $message
);
$gb = new Guestbook();
$gb->insert($data);
$this->_redirect('/');
}
} else $this->view->errorMessage = " , .";
}
function init(){
Zend_Loader::loadClass('Zend_Filter_StripTags');
Zend_Loader::loadClass('Guestbook');
Zend_Loader::loadClass('Zend_Paginator');
Zend_Loader::loadClass('Zend_Paginator_Adapter_array');
Zend_Loader::loadClass('Zend_View_Helper_PaginationControl');
}
}


We will not consider the addAction and init methods, because everything is trite there. We are interested in the indexAction, we will look at it in more detail.
$gb = new Guestbook();
$num = 10;
$page = $this->_getParam('page');
if($page<1 or empty($page)){$page = 1;}
$select = $gb->select()->from('guestbook',array('count'=>'COUNT(*)'))->order('id DESC ');
$total = $this->view->table = $gb->fetchRow($select)->toArray();
$start = $page*$num-$num;
$select = $gb->select()->from('guestbook')->order('id DESC ')->limit($start,$num);
$result = $this->view->table = $gb->fetchAll($select)->toArray();



$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_array($result));
Create an $ paginator object with its adapter.

$paginator->setCurrentPageNumber($page)
Here we set the number of the current page, which will be passed to the link as a parameter (/ index / page / <page number>).

$paginator->setView($this->view);
Set the view for the paginator, since we have the view that uses this action, we write $ this-> view.

$paginator->setItemCountPerPage($num);
Set the number of posts on one page. By default, it is already equal to ten (here it is done for example).

Zend_View_Helper_PaginationControl::setDefaultViewPartial('/my_pagination.phtml');
$this->view->paginator = $paginator;

Here we connect the helper file and transfer our paginator view. In the index.phtml file, it can be accessed as $ this-> paginator.

/application/scripts/index/index.phtml
<?php
if (count($this->paginator)): ?>
<?php foreach ($this->paginator as $item):?>
<?= $item['name']; ?><br>
<?= $item['message']; ?>
<?php endforeach; ?>
<?php endif; ?>

<?= $this->paginationControl($this->paginator, 'Sliding', '/my_pagination.phtml'); ?>

/application/scripts/index/index.phtml
<?php
if (count($this->paginator)): ?>
<?php foreach ($this->paginator as $item):?>
<?= $item['name']; ?><br>
<?= $item['message']; ?>
<?php endforeach; ?>
<?php endif; ?>

<?= $this->paginationControl($this->paginator, 'Sliding', '/my_pagination.phtml'); ?>


There is nothing unusual here except:
<?= $this->paginationControl($this->paginator, 'Sliding', '/my_pagination.phtml'); ?>
Instead of Sliding there can be the following parameters:

$ this-> paginator our object with a paginator - the third parameter indicates the path to the file my_pagination.phtml.
An approximate view of it may be:

/application/scripts/my_pagination.phtml
<?php
if ($this->pageCount): ?>
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
< Previous |
<?php endif; ?>
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<?php if (isset($this->next)): ?>
><?php else: ?>
<?php endif; ?>
<?php endif; ?>



$this->pageCount β€” $this->previous β€” $this->current β€” $this->next β€”
<a href="<?=$this->url(array('page' => $this->next)); ?>">>
, url(), , .

, , . , , Zend Framework.
/application/scripts/my_pagination.phtml
<?php
if ($this->pageCount): ?>
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
< Previous |
<?php endif; ?>
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<?php if (isset($this->next)): ?>
><?php else: ?>
<?php endif; ?>
<?php endif; ?>



$this->pageCount β€” $this->previous β€” $this->current β€” $this->next β€”
<a href="<?=$this->url(array('page' => $this->next)); ?>">>
, url(), , .

, , . , , Zend Framework.

Source: https://habr.com/ru/post/44422/


All Articles