如果你对ZF的手册上介绍的分页感到一头雾水的话,那么请看下文,看后你就会立刻喜欢上ZF的分页功能的:

 

Having a “search” functionality in your web application is very essential and for every search comes a “search results” page. It is not hard to create your own search results page with a pagination, but it does take a certain amount of time. But by using Zend_Paginator, you will be able to save time and make your code easier to understand and debug.

Here is a short example on how to create your own search results page with Zend_Paginator.

Before anything else, we start with our search form.

<form method="GET" action="<?=$this->baseUrl()?>/search">
SEARCH:
<input name="keyword" type="text"/>
<input type="submit" value="Search" />
</form>
Next, we have our Controller:

<?php
class SearchController extends Zend_Controller_Action
{
public function indexAction()
{
/**
* Load model and perform search if $keyword is found in the URL
*/
$content_model = new Default_Model_Content();
if($this->_request->getParams('keyword')) {
$results = $content_model->fetchSearchResults($this->_request->getParams('keyword'));
}

/**
* Setup the paginator if $results is set.
*/
if(isset($results)) {
$paginator = Zend_Paginator::factory($results);
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;
/**
* We will be using $this->view->paginator to loop thru in our view ;-)
*/

Zend_Paginator::setDefaultScrollingStyle('Sliding');
Zend_View_Helper_PaginationControl::setDefaultViewPartial(
'query/pagination.phtml' //Take note of this, we will be creating this file
);
}
}
Note: In this tutorial we are using LIKE to compare strings in our MySQL database. The search is very basic and there is no ranking in our results.


The Model
Our model will have 2 files. See below:

<?php
/**
* First model file.
* application/models/Content.php
*/
class Default_Model_Content extends Zend_Db_Table
{
protected $_table;

public function getTable()
{
if(null === $this->_table) {
$this->_table = new Default_Model_DbTable_Content();
}
return $this->_table;
}

public function fetchSearchResults($keyword)
{
$result = $this->getTable()->fetchSearchResults($keyword);
return $result;
}
And our second model file.

<?php
/**
* Second model file.
* application/models/DbTable/Content.php
*/
class Default_Model_DbTable_Orders extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'content';

public function init()
{
$this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
}

public function fetchSearchResults($keyword)
{
$sql = 'SELECT * FROM'.$this->_name.
'WHERE '.
$this->_db->quoteIdentifier('the_content').' '.
'LIKE'.
$this->_db->quote($keyword.'%');
return $this->_db->fetchAll($sql);
}


The View
Now that we have our controller and model ready, it is time to setup our view files.

First, create a file called index.phtml in application/views/scripts/search/:

<?php if($this->paginator): ?>
<table>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach($this->paginator AS $key => $row): ?>
<tr>
<td><?=$row->title?></td>
<td><?=$row->the_content?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->paginator; ?>
<?php else: ?>
No results.
<?php endif; ?>


The Pagination View File
This is the last step. Now all you have to do is to copy-paste this section of code into a file named: pagination.phtml
Create that file in: application/views/scripts/search/pagination.phtml

<!--
See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
-->

<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $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="<?php echo $this->url(array('page' => $page)); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<span id="current_page"><?php echo $page; ?></span> |
<?php endif; ?>
<?php endforeach; ?>

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

 

原文:http://blog.ekini.net/2009/06/22/zend-framework-how-to-use-zend_paginator/