加入收藏 | 设为首页 | 会员中心 | 我要投稿 南通站长网 (https://www.0513zz.cn/)- 专有云、图像技术、经验、数据治理、专属主机!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

非常好用的Zend Framework分页类

发布时间:2022-07-22 09:36:23 所属栏目:PHP教程 来源:互联网
导读:这篇文章主要介绍了非常好用的Zend Framework分页类,包含控制层、模型层、视图层及分页源码,Css部分请自由发挥,需要的朋友可以参考下。 在这里和大家分享一个非常好用的 Zend Framework 分页类,具体效果可见本站的分页效果, CSS样式可根据个人设计感进行更
这篇文章主要介绍了非常好用的Zend Framework分页类,包含控制层、模型层、视图层及分页源码,Css部分请自由发挥,需要的朋友可以参考下。
 
在这里和大家分享一个非常好用的 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 (emptyempty($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;
        } 

(编辑:南通站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读