Zend_Paginator Full 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 <strong style="color: black; background-color: #a0ffff;">table</strong>’;
$result = $db->fetchAll($sql);
$paginator = <strong style="color: black; background-color: #ffff66;">Zend_Paginator</strong>::factory($result);

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

$this->view->page_nr = $this->_getParam(’page’);

now let see the design list.phtml

<<strong style="color: black; background-color: #a0ffff;">table</strong>>
foreach($this->paginator as $row):  ?>
<td><?= $this->escape($row['col1']) ?></td>
<td><?= $this->escape($row['col2']) ?></td>
<td><?= $this->escape($row['col3']) ?></td>
<td><?= $this->escape($row['col4']) ?></td>
<? endforeach ?>
</<strong style="color: black; background-color: #a0ffff;">table</strong>>
<?= $this->paginationControl($this->paginator2, ‘Sliding’, ‘p.phtml’);?>

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;
<?php else: ?>
<span class=”disabled”>Next &gt;</span>
<?php endif; ?>
<?php endif; ?>

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

more information you can find here

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Comments are closed.