Zend Framework使用Zend_Paginator进行数据库交互和分页

时间:2022-08-27 11:07:48

一、大概思路

1.1

Zend Framework使用Zend_Paginator进行数据库交互和分页

(备注:具体配置和运行情况参考上一篇Ubuntu10.10 Zend FrameWork配置及helloworld显示)

二、创建数据库表

CREATE DATABASE IF NOT EXISTS test;
USE test;
DROP TABLE IF EXISTS `test`.`userinfo`;
CREATE TABLE `test`.`userinfo` (
`user_autoid`
int ( 11 ) NOT NULL AUTO_INCREMENT,
`
user_name ` varchar ( 20 ) NOT NULL ,
`user_pwd`
varchar ( 10 ) NOT NULL ,
PRIMARY KEY (`user_autoid`)
) ENGINE
= MyISAM AUTO_INCREMENT = 15 DEFAULT CHARSET = utf8;
INSERT INTO `test`.`userinfo` VALUES ( 1 , ' summer ' , ' 123 ' ),
(
2 , ' 1\ '' , ' 1 ' ),
(3,
' s ' , ' s ' ),
(4,
' ups ' , ' happy ' ),
(5,
' sdfsdfsd ' , ' sfdsdf ' ),
(6,
' s\ '' , ' ssssss ' ),
(
7 , ' sssssss ' , ' sssssss ' ),
(
8 , ' swyma ' , ' summerdir ' ),
(
9 , ' djb ' , ' ddd ' ),
(
10 , ' sss ' , ' sss ' ),
(
12 , ' CC ' , ' 112 ' ),
(
13 , ' c1 ' , ' 123 ' ),
(
14 , ' s1 ' , ' 234 ' );
UNLOCK TABLES;

三、创建项目zf create project pager

3.1、将application/configs/application.ini下的

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

resources.frontController.params.displayExceptions = 1

设置为1,Zend Framework可以提供调试错误信息的显示。

四、创建控制器、视图

4.1、indexController

<? php
class IndexController extends Zend_Controller_Action
{
private $_numPerPage = 5 ;
private $_pageRange = 5 ;
public function init ()
{
/* Initialize action controller here */
}
public function indexAction ()
{
$parm = $this -> _request -> getParam( ' parm ' );
if ( strtolower ( $_SERVER [ ' REQUEST_METHOD ' ]) == ' post ' ) {
// 取得前台得传过来的值
$text = ' % ' . $this -> _request -> getPost( ' txt ' ) . ' % ' ;
$db = Zend_Registry :: get( ' db ' );
// 搜索
$sql = $db -> quoteInto(
" SELECT * FROM userinfo where user_name like ? or user_pwd like ? " ,
$text );
// 分页
$numPerPage = $this -> _numPerPage;
$pageRange = $this -> _pageRange;
$page = $this -> _request -> getParam( ' page ' , 1 );
$offset = $numPerPage * $page ;
$db = new Application_Model_DbTable_UserInfo();
$select = $db -> getAllUserInfo( $sql ) -> fetchAll();
$paginator = Zend_Paginator :: factory( $select );
$paginator -> setCurrentPageNumber( $page )
-> setItemCountPerPage( $numPerPage )
-> setPageRange( $pageRange );
$this -> view -> userinfo = $paginator ;
$params = array ( ' parm ' => $this -> _request -> getPost( ' txt ' ));
$this -> _helper -> redirector( ' index ' , ' index ' , null ,
$params );
}
else
if ( $this -> _request -> getParam( ' parm ' ) != "" ) {
$text = ' % ' . $this -> _request -> getParam( ' parm ' ) . ' % ' ;
$db = Zend_Registry :: get( ' db ' );
// 搜索
$sql = $db -> quoteInto(
" SELECT * FROM userinfo where user_name like ? or user_pwd like ? " ,
$text );
// 分页
$numPerPage = $this -> _numPerPage;
$pageRange = $this -> _pageRange;
$page = $this -> _request -> getParam( ' page ' , 1 );
$offset = $numPerPage * $page ;
$db = new Application_Model_DbTable_UserInfo();
$select = $db -> getAllUserInfo( $sql ) -> fetchAll();
$paginator = Zend_Paginator :: factory( $select );
$paginator -> setCurrentPageNumber( $page )
-> setItemCountPerPage( $numPerPage )
-> setPageRange( $pageRange );
$this -> view -> userinfo = $paginator ;
}
else {
$db = new Application_Model_DbTable_UserInfo();
$sql = " SELECT * FROM userinfo " ;
// 分页
$numPerPage = $this -> _numPerPage;
$pageRange = $this -> _pageRange;
$page = $this -> _request -> getParam( ' page ' , 1 );
$offset = $numPerPage * $page ;
$db = new Application_Model_DbTable_Userinfo();
$select = $db -> getAllUserInfo( $sql ) -> fetchAll();
$paginator = Zend_Paginator :: factory( $select );
$paginator -> setCurrentPageNumber( $page )
-> setItemCountPerPage( $numPerPage )
-> setPageRange( $pageRange );
$this -> view -> userinfo = $paginator ;
}
}
public function pagelistAction ()
{
// action body
}
}

4.2、index.phtml视图

<! DOCTYPE unspecified PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " " http://www.w3.org/TR/html4/loose.dtd " >
< meta http - equiv = " Content-Type " content = " text/html; charset=UTF-8 " >
< form name = " login "
action
= " <?php
echo
$this ->url(array('controller' => 'index', 'action' => 'index'));
?>
"
method
= " post " >< input type = " text " name = " txt " />< input
type
= " submit " value = " 搜索 " > < input type = " button " value = " 刷新 "
onclick
= " location.href='<?php
echo
$this ->baseUrl()?>/index/index' " >
</ form >
< table >
< tr >
< th > 用户名 </ th >
< th > & nbsp;码 </ th >
</ tr >
<? php
foreach ( $this -> userinfo as $key => $value ) {
?>
< tr >
< td >
<? php
echo $value [ ' user_name ' ] ?>
</ td >
< td >
<? php
echo $value [ ' user_pwd ' ] ?>
</ td >
</ tr >
<? php
}
?>
</ table >
<!-- 页面样式
All:显示所有页;

Elastic:Google式,页码范围会根据用户当前页而扩展或缩小;

Jumping:页码最后页之后会显示第一页;

Sliding:Yahoo式,当前页会放在页码中间,这是默认样式。
-->
<? php
echo $this -> paginationControl( $this -> userinfo , ' Sliding ' ,
' /index/pagelist.phtml ' );
?>

4.3、pagelist.phtml

<? php if ($ this -> pageCount): ?>
< div class = " paginationControl " >
<? php if ( isset($ this -> first) ): ?>
< a href = " <?php echo $this->url(array('page'=>$this->first));?> " > 首页 </ a >
<? php else : ?>
< span class = " disabled " > 首页 </ span >
<? php endif; ?>

<? php if ( isset($ this -> previous) ): ?>
< a href = " <?php echo $this->url(array('page'=>$this->previous));?> " > 上一页 </ a >
<? php else : ?>
< span class = " disabled " > 上一页 </ span >
<? php endif; ?>
<? 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 : ?>
<? php echo $page; ?>|
<? php endif; ?>
<? php endforeach; ?>

<? php if (isset($ this -> next)): ?>
< a href = " <?php echo $this->url(array('page'=>$this->next));?> " > 下一页 </ a >
<? php else : ?>
< span class = " disabled " > 下一页 </ span >
<? php endif; ?>

<? php if ( isset($ this -> last) ): ?>
< a href = " <?php echo $this->url(array('page'=>$this->last));?> " > 尾页 </ a >
<? php else : ?>
< span class = " disabled " > 尾页 </ span >
<? php endif; ?>

< span > <? php echo $ this -> current; ?> </ span >
< span > <? php echo $ this -> pageCount; ?> </ span >
< span > <? php echo $ this -> totalItemCount; ?> </ span >
</ div >
<? php endif; ?>

4.4、Bootstrap.php(数据库连接)

如果连接出现mysql drivers not found,请检查是否安装了php5-mysql(在ubuntu下一般没有安装),到“新立德软件中心”安装

Zend Framework使用Zend_Paginator进行数据库交互和分页

<? php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDBConnection ()
{
// 数据库连接
$params = array ( ' host ' => ' localhost ' , ' username ' => ' root ' ,
' password ' => ' 123 ' , ' dbname ' => ' test ' , ' charset ' => ' utf8 ' );
$db = Zend_Db :: factory( ' PDO_MYSQL ' , $params );
Zend_Db_Table
:: setDefaultAdapter( $db );
Zend_Registry
:: set( ' db ' , $db );
}
}

五、运行效果

Zend Framework使用Zend_Paginator进行数据库交互和分页

Zend Framework使用Zend_Paginator进行数据库交互和分页

六、总结

Zend_Paginator还算是个不错的工具,挺方便的。

运行环境:ubuntu 10.10、zend framework

七、源代码下载

源代码下载:http://files.cnblogs.com/yongfeng/pager.zip