Zend Paginator Example

This is a small tutorial about How to make a pagination whit Zend Paginator

so we a have an action let say listAction

public function listAction()
{
$sql = ‘SELECT * FROM table’;
$result = $db->fetchAll($sql);
$paginator = Zend_Paginator::factory($result);

$paginator->setCurrentPageNumber($this->_getParam(‘page’));
$paginator->setItemCountPerPage(10); //where 10  is the number of records per page
$this->view->paginator = $paginator;

$this->view->page_nr = $this->_getParam(‘page’);
$this->render(‘list’);
}

now let see the design list.phtml

<div>
<table>
<tr>
<td>Col1</td>
<td>Col2</td>
<td>Col3</td>
<td>Col4</td>
</tr>
<?php
foreach($this->paginator as $row):  ?>
<tr>
<td><?= $this->escape($row['col1']) ?></td>
<td><?= $this->escape($row['col2']) ?></td>
<td><?= $this->escape($row['col3']) ?></td>
<td><?= $this->escape($row['col4']) ?></td>
</tr>
<? endforeach ?>
</table>
<?= $this->paginationControl($this->paginator2, ‘Sliding’, ‘p.phtml’);?>
</div>

the code for p.phtml

<?php if ($this->pageCount): ?>
<div class=”paginationControl”>
<!– Previous page link –>
<?php if (isset($this->previous)): ?>
<a href=”<?= $this->url(array(‘page’ => $this->previous)); ?>”>
&lt; Previous
</a> |
<?php else: ?>
<span class=”disabled”>&lt; Previous</span> |
<?php endif; ?>

<!– Numbered page links –>
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href=”<?= $this->url(array(‘page’ => $page)); ?>”>
<?= $page; ?>
</a> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>

<!– Next page link –>
<?php if (isset($this->next)): ?>
<a href=”<?= $this->url(array(‘page’ => $this->next)); ?>”>
Next &gt;
</a>
<?php else: ?>
<span class=”disabled”>Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>

p.phtml must be in the root of the template files,  main is application/views/scripts/

more information you can find here

You can leave a response, or trackback from your own site.

3 Responses to “Zend Paginator Example”

  1. SirNeo says:

    si o traducere ?

  2. amo says:

    La toti vad acelasi lucru, fetchAll in select. Pe o baza de 100.000 inregistrari chestia asta e doom curat, si problema e ca majoritatea articolelor de pe net merg exact in aceeasi idee.

    Mai eficient e de exemplu in model

    $select = $tabel->select();
    $select->order(array(‘id DESC’));
    $rows = Zend_Paginator::factory($select);
    $rows->setCurrentPageNumber(1);
    $rows->setItemCountPerPage(10);
    si un eventual $rows->setPageRange(5);

    apoi in logic ai

    foreach ($rows as $row) echo $row->id;

    dupa care in view $this->paginationControl(..)

    Am incercat sa nu gresesc nimic dar ma rog, ideea e de baza.

    Numai bine.

  3. victor says:

    e doar un exemplu de baza
    daca vorbesti de o tabela cu 100.000 de inregistrari, da ajungi la modele, ajungi la cache si altele :)

Leave a Reply

Powered by WordPress