A-A+

Zend Framework分页类用法详解

2021年07月21日 我爱编程 暂无评论

这篇文章主要介绍了Zend Framework分页类用法,结合实例形式详细分析了Zend Framework分页类的实现代码,相关功能与使用技巧,需要的朋友可以参考下,本文实例讲述了Zend Framework分页类用法,分享给大家供大家参考,具体如下:

1、分页类Pagination.php,最好是把这个类放在Zend目录下

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

2、在indexController.php中的indexController Function里面调用:

  1. require_once 'Zend/Pagination.php';
  2. $Users = new Users();
  3. //$rows = $Users->getAdapter()->fetchOne("select count(*) from users where `role`!='admin'"); //recorde count
  4. $rows = $Users->fetchAll("`role`!='admin'")->count(); //查询记录总数
  5. $rowsPerPage = 5; //perPage recordes
  6. $curPage = 1;
  7. if($this->_request->getParam('page'))
  8. {
  9.     $curPage = $this->_request->getParam('page');
  10. }
  11. //search data and display
  12. $this->view->users = $Users->fetchAll("`role`!='admin'",'id desc',$rowsPerPage,($curPage-1)*$rowsPerPage)->toArray();
  13. $Pager = new XY_Pagination($rows,$rowsPerPage);
  14. $this->view->pagebar = $Pager->getNavigation();

3、在view中调用分页更简单了。

pagebar?>

或者在smarty模板情况下

<{$pagebar}>

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录