Our blog
Zend Paginator – Actually Pretty Painless :)
With some trepidation I decided that I really had to implement pagination into a Zend Framework project I am working on. Zend Framework is great, but some of the sections can be a little tricky to get your head around at first attempt.
However, less than an hour after first looking at it, I have now got my system spitting out results in a nicely paginated ten at a time. The documentation on the Zend Framework reference is a little sparse but suffice to say that if you are using Zend_Db then actually its pretty easy.
The only bit that is a little tricky is getting the actual page controls to display, as you are left to code your own (though they do supply some code).
One major irritation with the Zend Framework reference guide is that I seem to be unable to copy and paste the code examples, it ends up looking like this:
-
<!--
-
2.
-
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
-
3.
-
-->
-
4.
-
-
5.
-
<?php if ($this->pageCount): ?>
-
6.
-
<div class="paginationControl">
-
7.
-
<!-- Previous page link -->
-
8.
-
9.
-
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
-
10.
-
<Previous
-
11.
-
</a> |
-
12.
note the line numbers, great.
this tutorial was really useful though:
Here is some code snippets:
Controller
-
public function viewAction(){
-
$this->view->input = $input = $this->_request->getPost('search');
-
$paginator = $this->_model->tableSearchAllPaginator($input);
-
$paginator->setCurrentPageNumber($this->_getParam('page'));
-
$this->view->paginator = $paginator;
-
}
Model
-
public function tableSearchAllPaginator($input, $fields_to_select=null){
-
$table = $this->getTable();
-
$select = $table->select();
-
$fields = $table->info(Zend_Db_Table_Abstract::COLS);
-
foreach($fields as $field) {
-
$select->orWhere("$field like ?", "%$input%");
-
}
-
}
-
$select->columns($fields_to_select);
-
}
-
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
-
return $paginator;
-
}
View Script
View Table Page Partial
-
echo '<table class="grid">';
-
foreach($this->paginator as $k=>$row) {
-
//EC_Debug::dump($k);
-
$row = $row->toArray();
-
//EC_Debug::diedump($row);
-
if($k == 0) {
-
echo '<tr>';
-
foreach($row as $f=>$v) {
-
echo "<th>$f</th>";
-
}
-
echo '</tr>';
-
}
-
echo '<tr>';
-
foreach($row as $f=>$v) {
-
$extra = '';
-
if($f=='id') {
-
$extra = '<a href="' . $this->url(array('controller'=>$this->controller, 'action'=>'edit', 'id'=>$v), null, true) . '"><img src="' . $this->baseUrl() . '/style/icons/application_edit.png" border="0" alt="edit"></a>';
-
$extra .= ' <a href="' . $this->url(array('controller'=>$this->controller, 'action'=>'delete', 'id'=>$v), null, true) . '"><img src="' . $this->baseUrl() . '/style/icons/cancel.png" border="0" alt="delete"></a>';
-
}
-
echo "<td>$extra $v</td>";
-
}
-
echo '</tr>';
-
}
-
echo '</table>';
-
}else{
-
echo 'no results...';
-
}
Pagination Controls Partial
-
<?php if ($this->pageCount): ?>
-
<div class="pagination">
-
-
<a href="<?= $this->url(array('page' => $this->previous)); ?>">« PREV</a> -
-
<?php endif; ?>
-
-
<?php
-
/* Page links */
-
-
foreach ($this->pagesInRange as $page): ?>
-
<a href="<?= $this->url(array('page' => $page)); ?>" <?php if($page == $this->current): ?>id="selected"><?php endif; ?><?= $page; ?></a>
-
<?php endforeach; ?>
-
-
- <a href="<?= $this->url(array('page' => $this->next)); ?>">Next></a>
-
<?php endif; ?>
-
-
</div>
-
<?php endif; ?>
RSS Feed