非常好用的Zend Framework分页类_php实例

在这里和大家分享一个非常好用的 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;

时间: 2024-08-30 03:33:15

非常好用的Zend Framework分页类_php实例的相关文章

Zend Framework分页类用法详解_php实例

本文实例讲述了Zend Framework分页类用法.分享给大家供大家参考,具体如下: 1.分页类Pagination.php,最好是把这个类放在Zend目录下 class XY_Pagination { private $_navigationItemCount = 10; //导航栏显示导航总页数 private $_pageSize = null; //每页项目数 private $_align = "right"; //导航栏显示位置 private $_itemCount =

phpfans留言版用到的数据操作类和分页类_php实例

复制代码 代码如下: class mysql{          function connect($dbhost, $dbuser, $dbpw, $dbname = '',$dbcharset='') {              if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {              $this->show('Can not connect to MySQL server');          }              

Zend Framework框架实现类似Google搜索分页效果_php实例

本文实例讲述了Zend Framework框架实现类似Google搜索分页效果.分享给大家供大家参考,具体如下: /** * * @param unknown_type $model 表类型 * @param unknown_type $ncontroller 那个控制器提交的controller * @param unknown_type $naction 那个action提交的action */ public function fenyepage2($model,$ncontroller,$

PHP实现适用于文件内容操作的分页类_php技巧

本文实例为大家分享了PHP实现文件内容操作的分页类,强调一下只针对文件的操作,供大家参考,具体内容如下 <?php class StrPage { private $current; //当前页 private $file; //操作文件 private $totalPage; //总的页数 private $url; //传递的参数 private $pageLen; //每页显示的长度 function __construct( $file,$len = 200 ){ $this->fil

PHP实现可自定义样式的分页类_php技巧

本文实例为大家分享了PHP实现可自定义样式的分页类,供大家参考,具体内容如下 <?php //namespace Component; /** * 2016-3-27 * @author ankang */ class Page { private $ShowPage; private $CountPage; private $Floorp; private $PageUrl; private $PageClass; private $CurClass; /** * @author ankang

两款万能的php分页类_php技巧

本文为大家分享个超级好用.万能的php分页类,具体的实现代码如下 第一款php分页类 <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * 分页类 * 使用方式: * $page = new Page(); * $page->init(1000, 20); * $page->setNotActiveTemplate('<

php自定义分页类完整实例_php技巧

本文实例讲述了php自定义分页类.分享给大家供大家参考,具体如下: <?php header("Content-type:text/html;Charset=utf-8"); class SubPages{ private $each_disNums;//每页显示的条目数 private $nums;//总条目数 private $current_page;//当前被选中的页 private $sub_pages;//每次显示的页数 private $pageNums;//总页数

zend framework配置操作数据库实例分析_php技巧

zendframework项目环境搭建后,看了下zend framework配置操作数据库,php教程如下: 在application/configs的文件下建立一个config.ini文件 配置信息如下: [general] db.adapter=PDO_MYSQL db.config.host=localhost/IParess db.config.username=username db.config.password=password db.config.dbname=databasen

php mysql数据库操作分页类_php技巧

复制代码 代码如下: <?php /*  *    mysql数据库 分页类  *    @package    pagelist  *    @author        yytcpt(无影)  *    @version    2008-03-27  *    @copyrigth    http://www.d5s.cn/   */ /*  *    分页样式     .page{float: left;font: 11px Arial, Helvetica, sans-serif; pa