Using Zend Framework’s Paginator Library
Posted: June 23rd, 2009 | Author: Dax | Filed under: PHP | Tags: pagination, zend framework, zend paginator | No Comments »Here’s a tutorial on how to use ZF’s paginator library.
Inside your controller:
public function displayResultsAction() {
// First condition gets filtered info from a form and passes it to the model
if($this->_request->getPost('Submit')) {
$dept_code= $this->_request->getPost('dept_code');
$results = $this->report_model->getReports($dept_code);
} elseif($this->_request->getParam('dept_code')) {
// Get the filtered info from the url params being passed by zend paginator
$dept_code= $this->_request->getParam('dept_code');
$results = $this->report_model->getReports($dept_code);
} else {
// do anything here...
// I just display the default report results
$dept_code = '';
$results = $this->report_model->getDefaultReports();
}
$page = $this->_request->getParam('page', 1);
$paginator = Zend_Paginator::factory($results);
//Set item count per page
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($page);
$this->view->results = $paginator;
$this->view->dept_code = $dept_code;
}
Inside your results view page:
<!-- html code here -->
<table class="view" cellpadding="3" cellspacing="0">
<thead>
<tr>
<th width="100">Id #</th>
<th width="150">Dept Code</th>
<th width="200">Department</th>
<th width="200">Project Name</th>
</tr>
</thead>
<tbody>
<?php foreach($this->results as $row) { ?>
<tr class="view-details">
<td><?=$row->Id?></td>
<td><?=$row->DeptCode?></td>
<td><?=$row->DeptName?></td>
<td><?=$row->ProjName?></td>
</tr>
<?php } ?>
</tbody>
</table>
<br>
<!-- specify path of pagination control under your views in the 3rd param -->
<!-- Adding extra params in the url is specified on the 4th function param here -->
<?=$this->paginationControl($this->results, 'Sliding', 'reports/results-pagination.phtml', array('dept_code' => $this->dept_code))?>
<!-- html code here -->
Inside your pagination file view:
<?php if ($this->pageCount): ?>
<div id="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<?php if($this->dept_code == '') { ?>
<a href="<?= $this->url(array('page' => $this->previous)); ?>">< Previous</a> |
<?php } else { ?>
<a href="<?= $this->url(array('page' => $this->previous, 'dept_code' => $this->dept_code)); ?>">< Previous</a> |
<?php } ?>
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?=$this->baseUrl()?>/reports/display-results?page=<?=$page?><?=($this->dept_code!= '') ? '&dept_code=' . $this->dept_code: ''?>"><?= $page; ?></a> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?=$this->baseUrl()?>/reports/display-results?page=<?=$this->next?><?=($this->dept_code != '') ? '&dept_code=' . $this->dept_code: ''?>">Next ></a>
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
Leave a Reply