当前位置:首页 > PHP教程 > php分页 > 列表

非常好用的Zend Framework分页类

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-02 15:38:13 浏览: 评论:0 

这篇文章主要介绍了非常好用的Zend Framework分页类,包含控制层、模型层、视图层及分页源码,Css部分请自由发挥,需要的朋友可以参考下。

在这里和大家分享一个非常好用的 Zend Framework 分页类,具体效果可见本站的分页效果, CSS样式可根据个人设计感进行更变。

这里我会举例演示如何使用该类, 如下:

IndexController.php, 在 Action 中写入如下代码:

  1. protected  $_curPage = 1;      //默认第一页 
  2. const PERPAGENUM     = 4;      //每页显示条目数 
  3.  
  4. public function indexAction() 
  5. {   
  6.     // $this->_blogModel 已实例化 blog Model 
  7.     // $rows -> 获得到所展示数据的总条目数 
  8.     $rows = $this->_blogModel->getTotalRows(); 
  9.       
  10.     if($pageNum = $this->getRequest()->getParam('page')) { 
  11.         //如果有值传入,覆盖初始的第一页 
  12.         $this->_curPage = $pageNum; 
  13.     } 
  14.       
  15.     //把数据表中的数据传到前端 
  16.     $this->view->blogInfo = $this->_blogModel->getBlogInfo( 
  17.                                 self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM 
  18.                             ); 
  19.     //实例化分页类,并传到前端 
  20.     $this->view->pagebar = $this->displayPageBar($rows); 
  21. } 
  22.  
  23. private function displayPageBar($totalRows) 
  24. { 
  25.     $Pager = new Zend_Pagination($totalRows,self::PERPAGENUM); 
  26.     return $Pager->getNavigation(); 
  27. } 

models/Blog.php,写入如下代码:

  1. public function getBlogInfo($perPageNum = NULL, $limit = NULL) 
  2. { 
  3.     return $this->fetchAll('1 = 1', 'blog_id desc', $perPageNum, $limit) 
  4.                 ->toArray(); 
  5. } 
  6.  
  7. public function getTotalRows($where = '1=1') 
  8. { 
  9.     return $this->fetchAll($where)->count(); 
  10. } 

index.phtml, 写入如下代码:

  1. <div class="page"> 
  2.     <!--?php echo $this--->pagebar; ?> 
  3. </div> 

到这里,就可以看见效果了, 如想追求更好的页面效果, 请根据个人喜好修改分页类,这里就不作详细示例,代码如下:

  1. class Zend_Pagination 
  2. { 
  3.     private $_navigationItemCount = 6;        //导航栏显示导航总页数 
  4.     private $_pageSize            = null;     //每页项目数 
  5.     private $_align               = "right";  //导航栏显示位置 
  6.     private $_itemCount           = null;     //总项目数 
  7.     private $_pageCount           = null;     //总页数 
  8.     private $_currentPage         = null;     //当前页 
  9.     private $_front               = null;     //前端控制器 
  10.     private $_PageParaName        = "page";   //页面参数名称 
  11.  
  12.     private $_firstPageString     = "|<<";    //导航栏中第一页显示的字符 
  13.     private $_nextPageString      = ">>";     //导航栏中前一页显示的字符 
  14.     private $_previousPageString  = "<<";     //导航栏中后一页显示的字符 
  15.     private $_lastPageString      = ">>|";    //导航栏中最后一页显示的字符 
  16.     private $_splitString         = " | ";    //页数字间的间隔符 
  17.  
  18.     public function __construct($itemCount, $pageSize) 
  19.     { 
  20.         if (!is_numeric($itemCount) || (!is_numeric($pageSize))) { 
  21.             throw new Exception("Pagination Error:not Number"); 
  22.         } 
  23.         $this->_itemCount = $itemCount; 
  24.         $this->_pageSize  = $pageSize; 
  25.         $this->_front     = Zend_Controller_Front::getInstance(); 
  26.  
  27.         $this->_pageCount = ceil($itemCount/$pageSize);   //总页数 
  28.         $page = $this->_front->getRequest()->getParam($this->_PageParaName); 
  29.           
  30.         if (emptyempty($page) || (!is_numeric($page))) {   
  31.             //为空或不是数字,设置当前页为1 
  32.             $this->_currentPage = 1; 
  33.         } else { 
  34.             if ($page < 1) { 
  35.                 $page = 1; 
  36.             } 
  37.             if ($page > $this->_pageCount) { 
  38.                 $page = $this->_pageCount; 
  39.             } 
  40.             $this->_currentPage = $page; 
  41.         } 
  42.     } 
  43.  
  44.     public function getCurrentPage() 
  45.     { 
  46.         return $this->_currentPage; 
  47.     } 
  48.  
  49.     public function getNavigation() 
  50.     { 
  51.         $navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">'; 
  52.           
  53.         //当前页处于第几栏分页 
  54.         $pageCote      = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;   
  55.         //总分页栏 
  56.         $pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1)); 
  57.         //分页栏中起始页 
  58.         $pageStart     = $pageCote * ($this->_navigationItemCount -1) + 1;  
  59.         //分页栏中终止页       
  60.         $pageEnd       = $pageStart + $this->_navigationItemCount - 1;                       
  61.           
  62.         if($this->_pageCount < $pageEnd) { 
  63.             $pageEnd   = $this->_pageCount; 
  64.         } 
  65.           
  66.         $navigation .= "总共: {$this->_itemCount} 条 共 {$this->_pageCount} 页\n  "; 
  67.           
  68.         if($pageCote > 0) {           //首页导航 
  69.             $navigation .= '<a href="'.$this->createHref(1) 
  70.                            ." \"="">$this->_firstPageString</a> "; 
  71.         } 
  72.         if($this->_currentPage != 1) {       //上一页导航 
  73.             $navigation .= '<a href="'.$this->createHref($this->_currentPage-1); 
  74.             $navigation .= " \"="">$this->_previousPageString</a> "; 
  75.         }else{ 
  76.             $navigation .= $this->_previousPageString . ' '; 
  77.         } 
  78.          
  79.         while ($pageStart <= $pageEnd)      //构造数字导航区 
  80.         { 
  81.             if ($pageStart == $this->_currentPage) { 
  82.                 $navigation .= "<b>$pageStart</b>" . $this->_splitString; 
  83.             } else { 
  84.                 $navigation .= '<a href="'.$this->createHref($pageStart) 
  85.                                ." \"="">$pageStart</a>" 
  86.                                . $this->_splitString; 
  87.             } 
  88.             $pageStart++; 
  89.         } 
  90.           
  91.         if($this->_currentPage != $this->_pageCount) {   //下一页导航 
  92.             $navigation .= ' <a href="' 
  93.                            . $this->createHref($this->_currentPage+1) 
  94.                            . " \"="">$this->_nextPageString</a> "; 
  95.         }else{ 
  96.             $navigation .= $this->_nextPageString; 
  97.         } 
  98.          
  99.         if ($pageCote < $pageCoteCount-1) {               //未页导航 
  100.             $navigation .= '<a href="' 
  101.                            . $this->createHref($this->_pageCount) 
  102.                            . " \"="">$this->_lastPageString</a> "; 
  103.         } 
  104.  
  105.         $navigation .= ' 到 <select onchange="window.location=\' ' 
  106.                        . $this->createHref() 
  107.                        . '\'+this.options[this.selectedIndex].value;">'; 
  108.           
  109.         for ($i=1;$i<=$this->_pageCount;$i++){ 
  110.             if ($this->getCurrentPage()==$i){ 
  111.                $selected = "selected"; 
  112.             } else { 
  113.                $selected = ""; 
  114.             } 
  115.             $navigation .= '<option value=" . $i . " '="" .="" $selected="">' 
  116.                            . $i 
  117.                            . '</option>'; 
  118.         } 
  119.         $navigation .= '</select>'; 
  120.         $navigation .= " 页</div>"; 
  121.         return $navigation; 
  122.     } 
  123.  
  124.     public function getNavigationItemCount() 
  125.     { 
  126.         return $this->_navigationItemCount; 
  127.     } 
  128.  
  129.     public function setNavigationItemCoun($navigationCount) 
  130.     { 
  131.         if(is_numeric($navigationCount)) { 
  132.             $this->_navigationItemCount = $navigationCount; 
  133.         } 
  134.     } 
  135.  
  136.     public function setFirstPageString($firstPageString) 
  137.     { 
  138.         $this->_firstPageString = $firstPageString; 
  139.     } 
  140.  
  141.     public function setPreviousPageString($previousPageString) 
  142.     { 
  143.         $this->_previousPageString = $previousPageString; 
  144.     } 
  145.  
  146.     public function setNextPageString($nextPageString) 
  147.     { 
  148.         $this->_nextPageString = $nextPageString; 
  149.     } 
  150.  
  151.     public function setLastPageString($lastPageString) 
  152.     { 
  153.         $this->_lastPageString = $lastPageString; 
  154.     } 
  155.  
  156.     public function setAlign($align) 
  157.     { 
  158.         $align = strtolower($align); 
  159.         if ($align == "center") { 
  160.             $this->_align = "center"; 
  161.         } elseif ($align == "right") { 
  162.             $this->_align = "right"; 
  163.         } else { 
  164.             $this->_align = "left"; 
  165.         } 
  166.     } 
  167.      
  168.     public function setPageParamName($pageParamName) 
  169.     { 
  170.         $this->_PageParaName = $pageParamName; 
  171.     } 
  172.  
  173.     public function getPageParamName() 
  174.     { 
  175.         return $this->_PageParaName; 
  176.     } 
  177.  
  178.     private function createHref($targetPage = null) 
  179.     { 
  180.         $params     = $this->_front->getRequest()->getParams(); 
  181.         $module     = $params["module"]; 
  182.         $controller = $params["controller"]; 
  183.         $action     = $params["action"]; 
  184.  
  185.         $targetUrl = $this->_front->getBaseUrl() 
  186.                      . "/$module/$controller/$action"; 
  187.                        
  188.         foreach ($params as $key => $value) 
  189.         { 
  190.             if($key != "controller" && $key != "module" 
  191.                && $key != "action" && $key != $this->_PageParaName) { 
  192.                 $targetUrl .= "/$key/$value"; 
  193.             } 
  194.         } 
  195.         if (isset($targetPage)) {                //指定目标页 
  196.             $targetUrl .= "/$this->_PageParaName/$targetPage"; 
  197.         } else { 
  198.             $targetUrl .= "/$this->_PageParaName/"; 
  199.         } 
  200.         return $targetUrl; 
  201.     } 
  202. } 

这里再简单回顾下 Mysql 中的 limit offset

假设数据库表 blog 存在 13 条数据。

语句1:select * from blog limit 9, 4

语句2:select * from blog limit 4 offset 9

语句1和2均返回表 blog 的第 10、11、12、13 行

语句1中的 9 表示从表的第十行开始, 返回 4 行

语句2中的 4 表示返回 4 行,offset 9 表示从表的第十行开始

如下语句显示分页效果:

  1. 语句3:select * from blog limit ($this->_curPage - 1)* self::PERPAGENUM, self::PERPAGENUM; 
  2. 语句4:select * from blog limit self::PERPAGENUM offset ($this->_curPage - 1) * self::PERPAGENUM; 

Tags: Zend Framework

分享到: