如果你对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.


SEARCH:



Next, we have our Controller:

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:

/**
* 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.

/**
* 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/:

paginator): ?>






paginator AS $key => $row): ?>





Title Content
title?> the_content?>

paginator; ?>

No results.


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



pageCount): ?>



previous)): ?>

< Previous
|

< Previous |



pagesInRange as $page): ?>
current): ?>


|

|




next)): ?>

Next >


Next >


 

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