欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Zend Framework分页类用法详解

程序员文章站 2024-04-01 23:12:22
本文实例讲述了zend framework分页类用法。分享给大家供大家参考,具体如下: 1、分页类pagination.php,最好是把这个类放在zend目录下...

本文实例讲述了zend framework分页类用法。分享给大家供大家参考,具体如下:

1、分页类pagination.php,最好是把这个类放在zend目录下

class xy_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;
    }
  }
  /**
   * 返回当前页
   * @param int 当前页
   */
  public function getcurrentpage()
  {
    return $this->_currentpage;
  }
  /**
   * 返回导航栏目
   * @return string 导航html class="pagenavigation"
   */
  public function getnavigation()
  {
    $navigation = '';
    $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 .= '$this->_firstpagestring ";
    }
    if($this->_currentpage != 1) //上一页导航
    {
      $navigation .= '$this->_previouspagestring ";
    }
    while ($pagestart <= $pageend) //构造数字导航区
    {
      if($pagestart == $this->_currentpage)
      {
        $navigation .= "$pagestart".$this->_splitstring;
      }
      else
      {
        $navigation .= '$pagestart".$this->_splitstring;
      }
      $pagestart++;
    }
    if($this->_currentpage != $this->_pagecount) //下一页导航
    {
      $navigation .= ' $this->_nextpagestring ";
    }
    if($pagecote < $pagecotecount-1) //未页导航
    {
      $navigation .= '$this->_lastpagestring ";
    }
    //添加直接导航框
    //$navigation .= '';
    //2008年8月27号补充输入非正确页码后出现的错误——begin
    $navigation .= ' ';
    //2008年8月27号补充输入非正确页码后出现的错误——end
    $navigation .= " ";
    return $navigation;
  }
  /**
   * 取得导航栏显示导航总页数
   *
   * @return int 导航栏显示导航总页数
   */
  public function getnavigationitemcount()
  {
    return $this->_navigationitemcount;
  }
  /**
   * 设置导航栏显示导航总页数
   *
   * @param int $navigationcount:导航栏显示导航总页数
   */
  public function setnavigationitemcoun($navigationcount)
  {
    if(is_numeric($navigationcount))
    {
      $this->_navigationitemcount = $navigationcount;
    }
  }
  /**
   * 设置首页显示字符
   * @param string $firstpagestring 首页显示字符
   */
  public function setfirstpagestring($firstpagestring)
  {
    $this->_firstpagestring = $firstpagestring;
  }
  /**
   * 设置上一页导航显示字符
   * @param string $previouspagestring:上一页显示字符
   */
  public function setpreviouspagestring($previouspagestring)
  {
    $this->_previouspagestring = $previouspagestring;
  }
  /**
   * 设置下一页导航显示字符
   * @param string $nextpagestring:下一页显示字符
   */
  public function setnextpagestring($nextpagestring)
  {
    $this->_nextpagestring = $nextpagestring;
  }
  /**
   * 设置未页导航显示字符
   * @param string $nextpagestring:未页显示字符
   */
  public function setlastpagestring($lastpagestring)
  {
    $this->_lastpagestring = $lastpagestring;
  }
  /**
   * 设置导航字符显示位置
   * @param string $align:导航位置
   */
  public function setalign($align)
  {
    $align = strtolower($align);
    if($align == "center")
    {
      $this->_align = "center";
    }elseif($align == "right")
    {
      $this->_align = "right";
    }else
    {
      $this->_align = "left";
    }
  }
  /**
   * 设置页面参数名称
   * @param string $pageparamname:页面参数名称
   */
  public function setpageparamname($pageparamname)
  {
    $this->_pageparaname = $pageparamname;
  }
  /**
   * 获取页面参数名称
   * @return string 页面参数名称
   */
  public function getpageparamname()
  {
    return $this->_pageparaname;
  }
  /**
   * 生成导航链接地址
   * @param int $targetpage:导航页
   * @return string 链接目标地址
   */
  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;
  }
}
?>

2、在indexcontroller.php中的indexcontroller function里面调用:

require_once 'zend/pagination.php';
$users = new users();
//$rows = $users->getadapter()->fetchone("select count(*) from users where `role`!='admin'"); //recorde count
$rows = $users->fetchall("`role`!='admin'")->count(); //查询记录总数
$rowsperpage = 5; //perpage recordes
$curpage = 1;
if($this->_request->getparam('page'))
{
    $curpage = $this->_request->getparam('page');
}
//search data and display
$this->view->users = $users->fetchall("`role`!='admin'",'id desc',$rowsperpage,($curpage-1)*$rowsperpage)->toarray();
$pager = new xy_pagination($rows,$rowsperpage);
$this->view->pagebar = $pager->getnavigation();

3、在view中调用分页更简单了。

pagebar?>

或者在smarty模板情况下

<{$pagebar}>

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于zend framework框架的php程序设计有所帮助。