非常好用的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 相关文章推荐
php 搜索框提示(自动完成)实例代码
Feb 05 PHP
php将gd生成的图片缓存到memcache的小例子
Jun 05 PHP
php抓取页面的几种方法详解
Jun 17 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(六)
Jun 23 PHP
Laravel 5 学习笔记
Mar 06 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
Dec 31 PHP
Zend Framework教程之Application用法实例详解
Mar 14 PHP
CI框架实现框架前后端分离的方法详解
Dec 30 PHP
利用PHPExcel读取Excel的数据和导出数据到Excel
May 12 PHP
PHP设计模式之工厂模式实例总结
Sep 01 PHP
利用PHP判断是手机移动端还是PC端访问的函数示例
Dec 14 PHP
laravel + vue实现的数据统计绘图(今天、7天、30天数据)
Jul 31 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函数常用用法小结
2010/02/08 PHP
php 广告调用类代码(支持Flash调用)
2011/08/11 PHP
php启用sphinx全文搜索的实现方法
2014/12/24 PHP
php跨服务器访问方法小结
2015/05/12 PHP
SSO单点登录的PHP实现方法(Laravel框架)
2016/03/23 PHP
Alliance vs Liquid BO3 第三场2.13
2021/03/10 DOTA
jquery 插件学习(五)
2012/08/06 Javascript
一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子
2014/06/26 Javascript
JavaScript中输出标签的方法
2014/08/27 Javascript
php,js,css字符串截取的办法集锦
2014/09/26 Javascript
JS插件overlib用法实例详解
2015/12/26 Javascript
全面解析多种Bootstrap图片轮播效果
2016/05/27 Javascript
EasyUI Combobox设置默认值 获取text的方法
2016/11/28 Javascript
Canvas 制作动态进度加载水球详解及实例代码
2016/12/09 Javascript
js Element Traversal规范中的元素遍历方法
2018/04/19 Javascript
element ui table(表格)实现点击一行展开功能
2018/12/04 Javascript
Vue组件间的通信pubsub-js实现步骤解析
2020/03/11 Javascript
[01:28]2014DOTA2国际邀请赛中国区预选赛四大豪门直升机抵达会场
2014/05/24 DOTA
[35:27]完美世界DOTA2联赛循环赛 GXR vs FTD BO2第二场 10.29
2020/10/29 DOTA
Python之eval()函数危险性浅析
2014/07/03 Python
Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
2018/02/23 Python
使用django-crontab实现定时任务的示例
2018/02/26 Python
对Python中range()函数和list的比较
2018/04/19 Python
儿童编程python入门
2018/05/08 Python
windows下搭建python scrapy爬虫框架步骤
2018/12/23 Python
Python列表推导式实现代码实例
2020/09/09 Python
html5中嵌入视频自动播放的问题解决
2020/05/25 HTML / CSS
Staples加拿大官方网站:办公用品一站式采购
2016/09/25 全球购物
英国最大的婴儿监视器网上商店:Baby Monitors Direct
2018/04/24 全球购物
Engel & Bengel官网:婴儿推车、儿童房家具和婴儿设备
2019/12/28 全球购物
环境科学专业个人求职信
2013/12/15 职场文书
应届毕业生个人求职信范文
2014/01/29 职场文书
12.4法制宣传日标语
2014/10/08 职场文书
2015年共青团工作总结
2015/05/15 职场文书
怒海潜将观后感
2015/06/11 职场文书
Python办公自动化解决world文件批量转换
2021/09/15 Python