现在的位置: 首页 > 综合 > 正文

Zend Framework 分页

2018年01月10日 ⁄ 综合 ⁄ 共 2665字 ⁄ 字号 评论关闭

1.Zend文件夹下新建文件Pagination.php

<?
class Pagination
{
    private $_navigationItemCount = 10;                //导航栏显示导航总页数
    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.'">';

        $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.'&nbsp;';
      

抱歉!评论已关闭.