添加页插件类: /** * 分页 * */ class Custom_Controller_Plugin_Page extends Zend_Controller_Plugin_Abstract { public $pagelimit;//一页有多少行 public $pagesum; //总行数 public $pageid; //当前页数 public $dbtable; //数据表类 public $pages; //总页数 public $rs; //当前页数据 public $pagebegin; //分部分 开始 public $pageend; //结束 public $prevpage; //上一页 public $nextpage; //下一页 public $pagestep = 5; public $where = ''; //条件 public $urlparam = null; //扩展传递变量附加到url中 public function __construct($pageid,$pagelimit=null,$db, array $param = null){ $this->dbtable = $db; $db=$db->getAdapter(); if(isset($param)){ $w = null; if(is_array($param)){ foreach ($param as $key=>$value){ $this->urlparam .= '/'.$key.'/'.$value; $w[] = $db->quoteInto($key.' = ?',$value); } } $this->where .= implode(' and ',$w); $this->pagesum = $db->fetchOne('select count(*) from '.$this->dbtable->getTable().' where '.$this->where); } else{ $this->pagesum = $db->fetchOne('select count(*) from '.$this->dbtable->getTable()); } if ($this->pagesum == 0) { return false; } $this->pagelimit =$pagelimit; $this->pages = ceil($this->pagesum/$this->pagelimit); if($pageid<1 || $pageid>$this->pages){ $smartyview = Zend_Registry::get('smartyview'); $smartyview->error ='错误的页码!'; echo $smartyview->render('error.html'); exit; } $this->pageid =$pageid; $this->prevpage = $pageid -1; $this->nextpage = $pageid +1; if ($pageid == 1) { $this->pagebegin = 1; } else{ $this->pagebegin = $pageid - floor($this->pagestep/2); } if($this->pagebegin < 1){ $this->pagebegin = 1; } $this->pageend = $pageid + floor($this->pagestep/2); if ($this->pageend<$this->pagestep){ $this->pageend = $this->pagestep; } if($this->pageend>$this->pages){ $this->pageend = $this->pages; } } /** * 得到数据数组 * * @param string $order * @return array */ public function getArray($order=null){ if ($this->pagesum == 0) { return false; } $db =$this->dbtable->getAdapter(); $limit = $this->pagelimit; $offset = ($this->pageid - 1)*$this->pagelimit; if ($this->where == ''){ $where = ''; } else { $where = ' where '.$this->where; } if ($order == null) { $rs = $db->fetchAll('select * from '.$this->dbtable->getTable().$where.' limit '.$limit.' offset '.$offset); } else { $rs = $db->fetchAll('select * from '.$this->dbtable->getTable().$where.' order by '.$order.' limit '.$limit.' offset '.$offset); } return $rs; } /** * 获得page.html * * @return string */ public function getResult(){ if ($this->pagesum == 0) { return false; } $smartyview = Zend_Registry::get('smartyview'); $smartyview->pageid = $this->pageid ; $smartyview->pages =$this->pages; $smartyview->prevpage=$this->prevpage; $smartyview->nextpage=$this->nextpage; $smartyview->pagebegin =$this->pagebegin; $smartyview->pageend =$this->pageend; $smartyview->pagesum = $this->pagesum; $smartyview->urlparam = $this->urlparam; return $smartyview->render('page.html'); } /** * 获得<{$page}> * */ public function getPage(){ $smartyview = Zend_Registry::get('smartyview'); $smartyview->page=$this->getResult(); } } view中的调用例子(smarty): <div id="page" align="left"> <a href="<{$siteUrl}>/page/1<{$urlparam}>/username/<{$blogname}>">头页</a> <{if $pageid!=1}> <a href="<{$siteUrl}>/page/<{$prevpage}><{$urlparam}>/username/<{$blogname}>">上一页</a> <{/if}> <{section loop=$pageend+1 name=loop start = $pagebegin }> <{if $smarty.section.loop.index == $pageid }> <a href="<{$siteUrl}>/page/<{$smarty.section.loop.index}><{$urlparam}>/username/<{$blogname}>" id ="pageid"><{$smarty.section.loop.index}></a> <{else}> <a href="<{$siteUrl}>/page/<{$smarty.section.loop.index}><{$urlparam}>/username/<{$blogname}>"><{$smarty.section.loop.index}></a> <{/if}> <{/section}> <{if $pageid != $pages}> <a href="<{$siteUrl}>/page/<{$nextpage}><{$urlparam}>/username/<{$blogname}>">下一页</a> <{/if}> <a href="<{$siteUrl}>/page/<{$pages}><{$urlparam}>/username/<{$blogname}>">尾页</a> <span>转到</span> <select οnchange="window.location.href='<{$siteUrl}>/page/'+this.value+'<{$urlparam}>/username/<{$blogname}>'"> <{section loop=$pages+1 name=loop start = 1 }> <{if $smarty.section.loop.index == $pageid}> <option value="<{$smarty.section.loop.index}>" selected ><{$smarty.section.loop.index}></option> <{else}> <option value="<{$smarty.section.loop.index}>" ><{$smarty.section.loop.index}></option> <{/if}> <{/section}> </select> <span>页</span> <span>共有数据<b> <{$pagesum}> </b>条,当前第<b> <{$pageid}> </b>页,总共有<b> <{$pages}> </b>页</span> </div>