您的位置:首页 > 编程语言 > PHP开发

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

2012-09-27 15:44 441 查看
创建数据库表

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"),
(11,"我是茂安","样册"),
(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可以提供pager的调试。

四、创建控制器、视图

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>密 码</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 $ctr="index";$act="index";?>
<?php if($this->pageCount):?>
<div class="paginationControl">
<?php if( isset($this->first) ):?>
<a href="<?php echo $this->url(array("controller"=>$ctr,"action"=> $act,"page"=>$this->first));?>">首页</a>
<?php else: ?>
<span class="disabled">首页</span>
<?php endif;?>

<?php if( isset($this->previous) ):?>
<a href="<?php echo $this->url(array("controller"=>$ctr,"action"=> $act,"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("controller"=>$ctr,"action"=>$act,"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("controller"=>$ctr,"action"=> $act,"page"=>$this->next));?>">下一页</a>
<?php else:?>
<span class="disabled" >下一页</span>
<?php endif;?>

<?php if( isset($this->last) ):?>
<a href="<?php echo $this->url(array("controller"=>$ctr,"action"=> $act,"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(数据库连接)

<?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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息