非常好用的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的开合式多级菜单程序
Oct 09 PHP
PHP自动更新新闻DIY
Oct 09 PHP
一段防盗连的PHP代码
Dec 06 PHP
PHP通用检测函数集合
Feb 08 PHP
一个基于PDO的数据库操作类
Mar 24 PHP
鸡肋的PHP单例模式应用详解
Jun 03 PHP
php+html5使用FormData对象提交表单及上传图片的方法
Feb 11 PHP
标准版Eclipse搭建PHP环境的详细步骤
Nov 18 PHP
PHP链表操作简单示例
Oct 15 PHP
Zend Framework框架中实现Ajax的方法示例
Jun 27 PHP
php的无刷新操作实现方法分析
Feb 28 PHP
php 解析非标准json、非规范json
Apr 01 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
MVC模式的PHP实现
2006/10/09 PHP
一个用mysql_odbc和php写的serach数据库程序
2006/10/09 PHP
apache配置虚拟主机的方法详解
2013/06/17 PHP
php通过获取头信息判断图片类型的方法
2015/06/26 PHP
PHP实现根据数组的值进行分组的方法
2017/04/20 PHP
经典的解除许多网站无法复制文字的绝招
2006/12/31 Javascript
ie 调试javascript的工具
2009/04/29 Javascript
ejs v9 javascript模板系统
2012/03/21 Javascript
js通过地址栏给action传值(中文乱码全是问号)
2013/05/02 Javascript
js 显示base64编码的二进制流网页图片
2014/04/04 Javascript
jQuery实现流动虚线框的方法
2015/01/29 Javascript
基于javascript实现图片左右切换效果
2016/01/25 Javascript
分析js闭包引起的事件注册问题
2016/03/29 Javascript
30分钟快速掌握Bootstrap框架
2016/05/24 Javascript
JS之相等操作符详解
2016/09/13 Javascript
jQuery扩展实现text提示还能输入多少字节的方法
2016/11/28 Javascript
jQuery层级选择器实例代码
2017/02/06 Javascript
React-router 4 按需加载的实现方式及原理详解
2017/05/25 Javascript
vue点击当前路由高亮小案例
2019/09/26 Javascript
HTML元素拖拽功能实现的完整实例
2020/12/04 Javascript
python 字符串split的用法分享
2013/03/23 Python
新手如何发布Python项目开源包过程详解
2019/07/11 Python
python列表的逆序遍历实现
2020/04/20 Python
python切割图片的示例
2020/11/12 Python
详解python os.path.exists判断文件或文件夹是否存在
2020/11/16 Python
Python爬虫之Selenium实现关闭浏览器
2020/12/04 Python
简历上的自我评价怎么写
2014/01/28 职场文书
理工学院学生自我鉴定
2014/02/23 职场文书
关于建议书的格式范文
2014/05/20 职场文书
医生个人年度总结
2015/02/28 职场文书
2019暑假阅读倡议书
2019/06/24 职场文书
2019入党申请书格式
2019/06/25 职场文书
2019年12月24日平安夜祝福语集锦
2019/12/24 职场文书
聊一聊Redis与MySQL双写一致性如何保证
2021/06/26 Redis
Netty客户端接入流程NioSocketChannel创建解析
2022/03/25 Java/Android
简单聊聊TypeScript只读修饰符
2022/04/06 Javascript