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

本文介绍如何使用 Zend Framework 实现数据库操作及分页功能,包括创建数据库表、配置错误显示、编写控制器和视图来展示分页数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建数据库表



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>密&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 $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); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值