A-A+

非常好用的Zend Framework分页类

2021年03月02日 我爱编程 暂无评论

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

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

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

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

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

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. public function getTotalRows($where = '1=1')
  7. {
  8.     return $this->fetchAll($where)->count();
  9. }

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

这里再简单回顾下 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;

给我留言

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

用户登录