非常好用的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 相关文章推荐
BBS(php &amp; mysql)完整版(二)
Oct 09 PHP
PHP语法速查表
Dec 06 PHP
php 上传文件类型判断函数(避免上传漏洞 )
Jun 08 PHP
php数据结构与算法(PHP描述) 快速排序 quick sort
Jun 21 PHP
PHP时间戳 strtotime()使用方法和技巧
Oct 29 PHP
ThinkPHP CURD方法之page方法详解
Jun 18 PHP
php使用ereg验证文件上传的方法
Dec 16 PHP
PHP模板引擎Smarty内建函数详解
Apr 11 PHP
PHP实现的浏览器检查类
Apr 11 PHP
php实现留言板功能(代码详解)
Mar 28 PHP
php用wangeditor3实现图片上传功能
Aug 22 PHP
php接口隔离原则实例分析
Nov 11 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
用函数读出数据表内容放入二维数组
2006/10/09 PHP
smarty实例教程
2006/11/19 PHP
phpmyadmin中配置文件现在需要绝密的短语密码的解决方法
2007/02/11 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(二)
2014/06/23 PHP
JS字符串处理实例代码
2013/08/05 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
javascript实现模拟时钟的方法
2015/05/13 Javascript
直接拿来用的15个jQuery代码片段
2015/09/23 Javascript
jQuery取得iframe中元素的常用方法详解
2016/01/14 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
基于JavaScript实现下拉列表左右移动代码
2017/02/07 Javascript
angular十大常见问题
2017/03/07 Javascript
vue-resourse将json数据输出实例
2017/03/08 Javascript
mac上配置Android环境变量的方法
2018/07/08 Javascript
Vue表单及表单绑定方法
2018/09/04 Javascript
在angularJs中进行数据遍历的2种方法
2018/10/08 Javascript
详解Vue前端对axios的封装和使用
2019/04/01 Javascript
React学习之JSX与react事件实例分析
2020/01/06 Javascript
删除目录下相同文件的python代码(逐级优化)
2012/05/25 Python
Python 专题四 文件基础知识
2017/03/20 Python
python 函数中的内置函数及用法详解
2019/07/02 Python
Python递归调用实现数字累加的代码
2020/02/25 Python
Python爬虫爬取糗事百科段子实例分享
2020/07/31 Python
如何快速理解python的垃圾回收机制
2020/09/01 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
Python虚拟环境virtualenv创建及使用过程图解
2020/12/08 Python
详解HTML5如何使用可选样式表为网站或应用添加黑暗模式
2020/04/07 HTML / CSS
Pam & Gela官网:美国性感前卫女装品牌
2018/07/19 全球购物
我的动漫时代的创业计划书范文
2014/01/27 职场文书
小学五年级学生评语
2014/04/22 职场文书
大学生志愿者活动总结
2014/06/27 职场文书
加强作风建设心得体会
2014/10/22 职场文书
2014年幼儿园后勤工作总结
2014/11/10 职场文书
2015年公司保安年终工作总结
2015/05/14 职场文书
python字符串的多行输出的实例详解
2021/06/08 Python
Tomcat弱口令复现及利用
2022/05/06 Servers