现在的位置: 首页 > 综合 > 正文

非常好用的zend framework 分页类

2013年08月01日 ⁄ 综合 ⁄ 共 7013字 ⁄ 字号 评论关闭

转载请注明出处:web开发站长站
分页类:(我是放在../library/下的)
PageClass.php

  1. <?php
  2. class PageClass
  3. {
  4.     private $_navigationItemCount = 10;                //导航栏显示导航总页数
  5.     private $_pageSize = null;                        //每页项目数
  6.     private $_align = "right";                        //导航栏显示位置
  7.     private $_itemCount = null;                        //总项目数
  8.     private $_pageCount = null;                        //总页数
  9.     private $_currentPage = null;                    //当前页
  10.     private $_front = null;                            //前端控制器
  11.     private $_PageParaName = "page";                //页面参数名称
  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.         $this->_itemCount = $itemCount;
  23.         $this->_pageSize = $pageSize;
  24.         $this->_front = Zend_Controller_Front::getInstance();
  25.         $this->_pageCount = ceil($itemCount/$pageSize);            //总页数
  26.         $page = $this->_front->getRequest()->getParam($this->_PageParaName);
  27.         if(empty($page) || (!is_numeric($page)))    //为空或不是数字,设置当前页为1
  28.         {
  29.             $this->_currentPage = 1;
  30.         }
  31.         else
  32.         {
  33.             if($page < 1)
  34.                 $page = 1;
  35.             if($page > $this->_pageCount)
  36.                 $page = $this->_pageCount;
  37.             $this->_currentPage = $page;
  38.         }
  39.     }
  40.     /**
  41.      * 返回当前页
  42.      * @param int 当前页
  43.      */
  44.     public function getCurrentPage()
  45.     {
  46.         return $this->_currentPage;
  47.     }
  48.     /**
  49.      * 返回导航栏目
  50.      * @return string 导航html   class="PageNavigation" 
  51.      */
  52.     public function getNavigation()
  53.     {
  54.         $navigation = '<div style="text-align:'.$this->_align.'">';
  55.         $pageCote = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;    //当前页处于第几栏分页
  56.         $pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));    //总分页栏
  57.         $pageStart = $pageCote * ($this->_navigationItemCount -1) + 1;                    //分页栏中起始页
  58.         $pageEnd = $pageStart + $this->_navigationItemCount - 1;                        //分页栏中终止页
  59.         if($this->_pageCount < $pageEnd)
  60.         {
  61.             $pageEnd = $this->_pageCount;
  62.         }
  63.    $navigation .= "总共:{$this->_itemCount}条 {$this->_pageCount}页/n";
  64.         if($pageCote > 0)                                //首页导航
  65.         {
  66.             $navigation .= '<a href="'.$this->createHref(1)."/">$this->_firstPageString</a> ";
  67.         }
  68.         if($this->_currentPage != 1)                    //上一页导航
  69.         {
  70.             $navigation .= '<a href="'.$this->createHref($this->_currentPage-1);
  71.             $navigation .= "/">$this->_previousPageString</a> ";
  72.         }
  73.         while ($pageStart <= $pageEnd)                    //构造数字导航区
  74.         {
  75.             if($pageStart == $this->_currentPage)
  76.             {
  77.                 $navigation .= "<strong>$pageStart</strong>".$this->_splitString;
  78.             }
  79.             else
  80.             {
  81.                 $navigation .= '<a href="'.$this->createHref($pageStart)."/">$pageStart</a>".$this->_splitString;
  82.             }
  83.             $pageStart++;
  84.         }
  85.         if($this->_currentPage != $this->_pageCount)    //下一页导航
  86.         {
  87.             $navigation .= ' <a href="'.$this->createHref($this->_currentPage+1)."/">$this->_nextPageString</a> ";
  88.         }
  89.         if($pageCote < $pageCoteCount-1)                //未页导航
  90.         {
  91.             $navigation .= '<a href="'.$this->createHref($this->_pageCount)."/">$this->_lastPageString</a> ";
  92.         }
  93.         //添加直接导航框
  94.         //$navigation .= '<input type="text" size="3" onkeydown="if(event.keyCode==13){window.location=/' ';
  95.         //$navigation .= $this->createHref().'/'+this.value;return false;}" />';
  96.         //2008年8月27号补充输入非正确页码后出现的错误——begin
  97.         $navigation .= '  <select onchange="window.location=/' '.$this->createHref().'/'+this.options[this.selectedIndex].value;">';
  98.         for ($i=1;$i<=$this->_pageCount;$i++){
  99.         if ($this->getCurrentPage()==$i){
  100.            $selected = "selected";
  101.         }
  102.         else {
  103.            $selected = "";
  104.         }
  105.         $navigation .= '<option value='.$i.' '.$selected.'>'.$i.'</option>';
  106.         }
  107.         $navigation .= '</select>';
  108.         //2008年8月27号补充输入非正确页码后出现的错误——end
  109.         $navigation .= "</div>";
  110.         return $navigation;
  111.     }
  112.     /**
  113.      * 取得导航栏显示导航总页数
  114.      *
  115.      * @return int 导航栏显示导航总页数
  116.      */
  117.     public function getNavigationItemCount()
  118.     {
  119.         return $this->_navigationItemCount;
  120.     }
  121.     /**
  122.      * 设置导航栏显示导航总页数
  123.      *
  124.      * @param int $navigationCount:导航栏显示导航总页数
  125.      */
  126.     public function setNavigationItemCoun($navigationCount)
  127.     {
  128.         if(is_numeric($navigationCount))
  129.         {
  130.             $this->_navigationItemCount = $navigationCount;
  131.         }
  132.     }
  133.     /**
  134.      * 设置首页显示字符
  135.      * @param string $firstPageString 首页显示字符
  136.      */
  137.     public function setFirstPageString($firstPageString)
  138.     {
  139.         $this->_firstPageString = $firstPageString;
  140.     }
  141.     /**
  142.      * 设置上一页导航显示字符
  143.      * @param string $previousPageString:上一页显示字符
  144.      */
  145.     public function setPreviousPageString($previousPageString)
  146.     {
  147.         $this->_previousPageString = $previousPageString;
  148.     }
  149.     /**
  150.      * 设置下一页导航显示字符
  151.      * @param string $nextPageString:下一页显示字符
  152.      */
  153.     public function setNextPageString($nextPageString)
  154.     {
  155.         $this->_nextPageString = $nextPageString;
  156.     }
  157.     /**
  158.      * 设置未页导航显示字符
  159.      * @param string $nextPageString:未页显示字符
  160.      */
  161.     public function setLastPageString($lastPageString)
  162.     {
  163.         $this->_lastPageString = $lastPageString;
  164.     }
  165.     /**
  166.      * 设置导航字符显示位置
  167.      * @param string $align:导航位置
  168.      */
  169.     public function setAlign($align)
  170.     {
  171.         $align = strtolower($align);
  172.         if($align == "center")
  173.         {
  174.             $this->_align = "center";
  175.         }elseif($align == "right")
  176.         {
  177.             $this->_align = "right";
  178.         }else
  179.         {
  180.             $this->_align = "left";
  181.         }
  182.     }
  183.     /**
  184.      * 设置页面参数名称
  185.      * @param string $pageParamName:页面参数名称
  186.      */
  187.     public function setPageParamName($pageParamName)
  188.     {
  189.         $this->_PageParaName = $pageParamName;
  190.     }
  191.     /**
  192.      * 获取页面参数名称
  193.      * @return string 页面参数名称
  194.      */
  195.     public function getPageParamName()
  196.     {
  197.         return $this->_PageParaName;
  198.     }
  199.     /**
  200.      * 生成导航链接地址
  201.      * @param int $targetPage:导航页
  202.      * @return string 链接目标地址
  203.      */
  204.     private function createHref($targetPage = null)
  205.     {
  206.         $params = $this->_front->getRequest()->getParams();
  207.    $module = $params["module"];
  208.         $controller = $params["controller"];
  209.         $action = $params["action"];
  210.         $targetUrl = $this->_front->getBaseUrl()."/$module/$controller/$action";
  211.         foreach ($params as $key => $value)
  212.         {
  213.             if($key != "controller" && $key != "module" && $key != "action" && $key != $this->_PageParaName)
  214.             {
  215.                 $targetUrl .= "/$key/$value";
  216.             }
  217.         }
  218.         if(isset($targetPage))                //指定目标页
  219.             $targetUrl .= "/$this->_PageParaName/$targetPage";
  220.         else
  221.             $targetUrl .= "/$this->_PageParaName/";
  222.         return $targetUrl;
  223.     }
  224. }
  225. ?>

复制代码

调用实例代码:
我是在IndexController.php 控制器中用的。在列表方法里。

  1. function indexAction() {
  2.                 $this->view->title = '留言列表';
  3.                 $lybs = new Lybs();
  4.                 $order = 'uptime DESC';
  5.                 $rows = $lybs->fetchAll()->count();         //查询记录总数
  6.                 $rowsPerPage = 5;    //perPage recordes  
  7.                 $curPage = 1;
  8.                 if($this->_request->getParam('page')) {
  9.                    $curPage = $this->_request->getParam('page');
  10.                 }
  11.                 //search data and display
  12.                 $this->view->lybs = $lybs->fetchAll($where = null, $order,$rowsPerPage,($curPage-1)*$rowsPerPage);
  13.                 $Pager = new PageClass($rows,$rowsPerPage); 
  14.                 $this->view->pagebar = $Pager->getNavigation();                
  15.         }

复制代码

抱歉!评论已关闭.