非常好用的Zend Framework分页类


Posted in PHP onJune 25, 2014

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

这里我会举例演示如何使用该类, 如下:
 
IndexController.php, 在 Action 中写入如下代码:

protected  $_curPage = 1;      //默认第一页

const PERPAGENUM     = 4;      //每页显示条目数

 

public function indexAction()

{   

    // $this->_blogModel 已实例化 blog Model

    // $rows -> 获得到所展示数据的总条目数

    $rows = $this->_blogModel->getTotalRows();

     

    if($pageNum = $this->getRequest()->getParam('page')) {

        //如果有值传入,覆盖初始的第一页

        $this->_curPage = $pageNum;

    }

     

    //把数据表中的数据传到前端

    $this->view->blogInfo = $this->_blogModel->getBlogInfo(

                                self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM

                            );

    //实例化分页类,并传到前端

    $this->view->pagebar = $this->displayPageBar($rows);

}

 

private function displayPageBar($totalRows)

{

    $Pager = new Zend_Pagination($totalRows,self::PERPAGENUM);

    return $Pager->getNavigation();

}

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

public function getBlogInfo($perPageNum = NULL, $limit = NULL)

{

    return $this->fetchAll('1 = 1', 'blog_id desc', $perPageNum, $limit)

                ->toArray();

}

 

public function getTotalRows($where = '1=1')

{

    return $this->fetchAll($where)->count();

}

index.phtml, 写入如下代码:

<div class="page">

    <!--?php echo $this--->pagebar; ?>

</div>

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

class Zend_Pagination

{

    private $_navigationItemCount = 6;        //导航栏显示导航总页数

    private $_pageSize            = null;     //每页项目数

    private $_align               = "right";  //导航栏显示位置

    private $_itemCount           = null;     //总项目数

    private $_pageCount           = null;     //总页数

    private $_currentPage         = null;     //当前页

    private $_front               = null;     //前端控制器

    private $_PageParaName        = "page";   //页面参数名称

 

    private $_firstPageString     = "|<<";    //导航栏中第一页显示的字符

    private $_nextPageString      = ">>";     //导航栏中前一页显示的字符

    private $_previousPageString  = "<<";     //导航栏中后一页显示的字符

    private $_lastPageString      = ">>|";    //导航栏中最后一页显示的字符

    private $_splitString         = " | ";    //页数字间的间隔符

 

    public function __construct($itemCount, $pageSize)

    {

        if (!is_numeric($itemCount) || (!is_numeric($pageSize))) {

            throw new Exception("Pagination Error:not Number");

        }

        $this->_itemCount = $itemCount;

        $this->_pageSize  = $pageSize;

        $this->_front     = Zend_Controller_Front::getInstance();

 

        $this->_pageCount = ceil($itemCount/$pageSize);   //总页数

        $page = $this->_front->getRequest()->getParam($this->_PageParaName);

         

        if (empty($page) || (!is_numeric($page))) {   

            //为空或不是数字,设置当前页为1

            $this->_currentPage = 1;

        } else {

            if ($page < 1) {

                $page = 1;

            }

            if ($page > $this->_pageCount) {

                $page = $this->_pageCount;

            }

            $this->_currentPage = $page;

        }

    }

 

    public function getCurrentPage()

    {

        return $this->_currentPage;

    }

 

    public function getNavigation()

    {

        $navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">';

         

        //当前页处于第几栏分页

        $pageCote      = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;   

        //总分页栏

        $pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));

        //分页栏中起始页

        $pageStart     = $pageCote * ($this->_navigationItemCount -1) + 1;  

        //分页栏中终止页       

        $pageEnd       = $pageStart + $this->_navigationItemCount - 1;                       

         

        if($this->_pageCount < $pageEnd) {

            $pageEnd   = $this->_pageCount;

        }

         

        $navigation .= "总共: {$this->_itemCount} 条 共 {$this->_pageCount} 页\n  ";

         

        if($pageCote > 0) {           //首页导航

            $navigation .= '<a href="'.$this->createHref(1)

                           ." \"="">$this->_firstPageString</a> ";

        }

        if($this->_currentPage != 1) {       //上一页导航

            $navigation .= '<a href="'.$this->createHref($this->_currentPage-1);

            $navigation .= " \"="">$this->_previousPageString</a> ";

        }else{

            $navigation .= $this->_previousPageString . ' ';

        }

        

        while ($pageStart <= $pageEnd)      //构造数字导航区

        {

            if ($pageStart == $this->_currentPage) {

                $navigation .= "<b>$pageStart</b>" . $this->_splitString;

            } else {

                $navigation .= '<a href="'.$this->createHref($pageStart)

                               ." \"="">$pageStart</a>"

                               . $this->_splitString;

            }

            $pageStart++;

        }

         

        if($this->_currentPage != $this->_pageCount) {   //下一页导航

            $navigation .= ' <a href="'

                           . $this->createHref($this->_currentPage+1) 

                           . " \"="">$this->_nextPageString</a> ";

        }else{

            $navigation .= $this->_nextPageString;

        }

        

        if ($pageCote < $pageCoteCount-1) {               //未页导航

            $navigation .= '<a href="'

                           . $this->createHref($this->_pageCount) 

                           . " \"="">$this->_lastPageString</a> ";

        }

 

        $navigation .= ' 到 <select onchange="window.location=\' '

                       . $this->createHref()

                       . '\'+this.options[this.selectedIndex].value;">';

         

        for ($i=1;$i<=$this->_pageCount;$i++){

            if ($this->getCurrentPage()==$i){

               $selected = "selected";

            } else {

               $selected = "";

            }

            $navigation .= '<option value=" . $i . " '="" .="" $selected="">' 

                           . $i

                           . '</option>';

        }

        $navigation .= '</select>';

        $navigation .= " 页</div>";

        return $navigation;

    }

 

    public function getNavigationItemCount()

    {

        return $this->_navigationItemCount;

    }

 

    public function setNavigationItemCoun($navigationCount)

    {

        if(is_numeric($navigationCount)) {

            $this->_navigationItemCount = $navigationCount;

        }

    }

 

    public function setFirstPageString($firstPageString)

    {

        $this->_firstPageString = $firstPageString;

    }

 

    public function setPreviousPageString($previousPageString)

    {

        $this->_previousPageString = $previousPageString;

    }

 

    public function setNextPageString($nextPageString)

    {

        $this->_nextPageString = $nextPageString;

    }

 

    public function setLastPageString($lastPageString)

    {

        $this->_lastPageString = $lastPageString;

    }

 

    public function setAlign($align)

    {

        $align = strtolower($align);

        if ($align == "center") {

            $this->_align = "center";

        } elseif ($align == "right") {

            $this->_align = "right";

        } else {

            $this->_align = "left";

        }

    }

    

    public function setPageParamName($pageParamName)

    {

        $this->_PageParaName = $pageParamName;

    }

 

    public function getPageParamName()

    {

        return $this->_PageParaName;

    }

 

    private function createHref($targetPage = null)

    {

        $params     = $this->_front->getRequest()->getParams();

        $module     = $params["module"];

        $controller = $params["controller"];

        $action     = $params["action"];

 

        $targetUrl = $this->_front->getBaseUrl() 

                     . "/$module/$controller/$action";

                      

        foreach ($params as $key => $value)

        {

            if($key != "controller" && $key != "module"

               && $key != "action" && $key != $this->_PageParaName) {

                $targetUrl .= "/$key/$value";

            }

        }

        if (isset($targetPage)) {                //指定目标页

            $targetUrl .= "/$this->_PageParaName/$targetPage";

        } else {

            $targetUrl .= "/$this->_PageParaName/";

        }

        return $targetUrl;

    }

}

这里再简单回顾下 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 表示从表的第十行开始

如下语句显示分页效果:

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

PHP 相关文章推荐
漂亮但不安全的CTB
Oct 09 PHP
phpmyadmin中配置文件现在需要绝密的短语密码的解决方法
Feb 11 PHP
PHP 反射机制实现动态代理的代码
Oct 22 PHP
php下保存远程图片到本地的办法
Aug 08 PHP
nginx+php-fpm配置文件的组织结构介绍
Nov 07 PHP
php根据身份证号码计算年龄的实例代码
Jan 18 PHP
Php中使用Select 查询语句的实例
Feb 19 PHP
php模仿asp Application对象在线人数统计实现方法
Jan 04 PHP
PHP与服务器文件系统的简单交互
Oct 21 PHP
详解Yii2高级版引入bootstrap.js的一个办法
Mar 21 PHP
PHP判断密码强度的方法详解
May 26 PHP
PHP设计模式之模板方法模式定义与用法详解
Apr 02 PHP
PHP生成等比缩略图类和自定义函数分享
Jun 25 #PHP
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
Jun 25 #PHP
PHP内置过滤器FILTER使用实例
Jun 25 #PHP
PHP生成图片验证码、点击切换实例
Jun 25 #PHP
PHP生成随机密码类分享
Jun 25 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十二)
Jun 25 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 #PHP
You might like
使用PHP求两个文件的相对路径
2013/06/20 PHP
10个超级有用的PHP代码片段果断收藏
2015/09/23 PHP
php模板引擎技术简单实现
2016/03/15 PHP
javascript之querySelector和querySelectorAll使用说明
2011/10/09 Javascript
jqTransform form表单美化插件使用方法
2012/07/05 Javascript
使用apply方法处理数组的三个技巧[译]
2012/09/20 Javascript
Js点击弹出下拉菜单效果实例
2013/08/12 Javascript
node.js中使用socket.io制作命名空间
2014/12/15 Javascript
jQuery实现简单的间隔向上滚动效果
2015/03/09 Javascript
jQuery使用append在html元素后同时添加多项内容的方法
2015/03/26 Javascript
基于JavaScript实现TAB标签效果
2016/01/12 Javascript
jQuery技巧之让任何组件都支持类似DOM的事件管理
2016/04/05 Javascript
浅析jQuery 3.0中的Data
2016/06/14 Javascript
jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
2016/06/28 Javascript
AngularJS基础 ng-non-bindable 指令详细介绍
2016/08/02 Javascript
深入理解Angular2 模板语法
2016/08/07 Javascript
node实现简单的反向代理服务器
2017/07/26 Javascript
webpack写jquery插件的环境配置
2017/12/21 jQuery
vue 引入公共css文件的简单方法(推荐)
2018/01/20 Javascript
详解js跨域请求的两种方式,支持post请求
2018/05/05 Javascript
详解如何更好的使用module vuex
2019/03/27 Javascript
jquery插件实现轮播图效果
2020/10/19 jQuery
python利用rsa库做公钥解密的方法教程
2017/12/10 Python
Python 读取某个目录下所有的文件实例
2018/06/23 Python
pandas去重复行并分类汇总的实现方法
2019/01/29 Python
Python socket模块实现的udp通信功能示例
2019/04/10 Python
如何使用repr调试python程序
2020/02/28 Python
OpenCV 使用imread()函数读取图片的六种正确姿势
2020/07/09 Python
一款纯css3实现的颜色渐变按钮的代码教程
2014/11/12 HTML / CSS
工程地质勘察专业大学生求职信
2013/10/13 职场文书
计算机科学与技术专业求职信
2014/09/03 职场文书
2015年家长学校工作总结
2015/04/22 职场文书
新闻稿件写作范文
2015/07/18 职场文书
创业计划书之酒店
2019/08/30 职场文书
python实现求纯色彩图像的边框
2021/04/08 Python
Java虚拟机内存结构及编码实战分享
2022/04/07 Java/Android