非常好用的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 面向对象之成员方法详解
May 04 PHP
关于svn冲突的解决方法
Jun 21 PHP
php smarty模板引擎的6个小技巧
Apr 24 PHP
百度地图API应用之获取用户的具体位置
Jun 10 PHP
解决php的“It is not safe to rely on the system’s timezone settings”问题
Oct 08 PHP
如何在旧的PHP系统中使用PHP 5.3之后的库
Dec 02 PHP
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
Dec 08 PHP
PHP检测用户是否关闭浏览器的方法
Feb 14 PHP
php使用escapeshellarg时中文被过滤的解决方法
Jul 10 PHP
php错误日志简单配置方法
Jul 11 PHP
Yii编程开发常见调用技巧集锦
Jul 15 PHP
php基于 swoole 实现的异步处理任务功能示例
Aug 13 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
基于mysql的论坛(4)
2006/10/09 PHP
建站常用13种PHP开源CMS比较
2009/08/23 PHP
比file_get_contents稳定的curl_get_contents分享
2012/01/11 PHP
Php header()函数语法及使用代码
2013/11/04 PHP
Yii2使用小技巧之通过 Composer 添加 FontAwesome 字体资源
2014/06/22 PHP
php代码调试利器firephp安装与使用方法分析
2018/08/21 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
Jquery 弹出层插件实现代码
2009/10/24 Javascript
动感效果的TAB选项卡jquery 插件
2011/07/09 Javascript
定时器(setTimeout/setInterval)调用带参函数失效解决方法
2013/03/26 Javascript
解决IE6的PNG透明JS插件使用介绍
2013/04/17 Javascript
Bootstrap实现下拉菜单效果
2016/04/29 Javascript
jQuery纵向导航菜单效果实现方法
2016/12/19 Javascript
BootStrap 动态表单效果
2017/06/02 Javascript
js 事件的传播机制(实例讲解)
2017/07/20 Javascript
vue生成token保存在客户端localStorage中的方法
2017/10/25 Javascript
用React实现一个完整的TodoList的示例代码
2017/10/30 Javascript
react native 获取地理位置的方法示例
2018/08/28 Javascript
JS实现换肤功能的方法实例详解
2019/01/30 Javascript
详解微信小程序缓存--缓存时效性
2019/05/02 Javascript
[01:45]绝对公平!DOTA2队长征召模式详解
2014/04/25 DOTA
Python数据报表之Excel操作模块用法分析
2019/03/11 Python
Python脚本如何在bilibili中查找弹幕发送者
2020/06/04 Python
Python sorted对list和dict排序
2020/06/09 Python
Python环境管理virtualenv&amp;virtualenvwrapper的配置详解
2020/07/01 Python
Python configparser模块应用过程解析
2020/08/14 Python
非常漂亮的CSS3百叶窗焦点图动画
2016/02/24 HTML / CSS
美国女孩洋娃娃店:American Girl
2017/10/24 全球购物
W Hamond官网:始于1979年的钻石专家
2020/07/20 全球购物
思想品德课教学反思
2014/02/10 职场文书
延安红色之旅心得体会
2014/10/07 职场文书
工程主管竞聘书
2015/09/15 职场文书
关于感恩的歌曲整理(8首)
2019/08/14 职场文书
python爬虫之爬取笔趣阁小说
2021/04/22 Python
纯html+css实现打字效果
2021/08/02 HTML / CSS
Java Spring Boot 正确读取配置文件中的属性的值
2022/04/20 Java/Android