zend framework 分页类

 添加页插件类:

  1. /** 
  2.  * 分页 
  3.  * 
  4.  */  
  5. class Custom_Controller_Plugin_Page extends Zend_Controller_Plugin_Abstract {  
  6.     public  $pagelimit;//一页有多少行  
  7.     public  $pagesum;   //总行数  
  8.     public $pageid;   //当前页数  
  9.     public $dbtable;     //数据表类  
  10.     public $pages;    //总页数  
  11.     public $rs; //当前页数据  
  12.     public $pagebegin; //分部分 开始  
  13.     public $pageend;   //结束  
  14.     public $prevpage; //上一页  
  15.     public $nextpage; //下一页  
  16.     public $pagestep = 5;  
  17.     public $where = ''//条件  
  18.     public $urlparam = null;  //扩展传递变量附加到url中  
  19.     public function __construct($pageid,$pagelimit=null,$db, array $param = null){  
  20.         $this->dbtable = $db;  
  21.         $db=$db->getAdapter();  
  22.         if(isset($param)){  
  23.             $w = null;  
  24.             if(is_array($param)){  
  25.                 foreach ($param as $key=>$value){  
  26.                     $this->urlparam .= '/'.$key.'/'.$value;  
  27.                     $w[] = $db->quoteInto($key.' = ?',$value);  
  28.                 }  
  29.             }  
  30.             $this->where .= implode(' and ',$w);  
  31.               
  32.             $this->pagesum = $db->fetchOne('select count(*) from '.$this->dbtable->getTable().' where '.$this->where);         
  33.         }  
  34.         else{  
  35.         $this->pagesum = $db->fetchOne('select count(*) from '.$this->dbtable->getTable());  
  36.         }  
  37.         if ($this->pagesum == 0) {  
  38.              return false;  
  39.         }  
  40.         $this->pagelimit =$pagelimit;  
  41.         $this->pages = ceil($this->pagesum/$this->pagelimit);  
  42.         if($pageid<1 || $pageid>$this->pages){  
  43.             $smartyview = Zend_Registry::get('smartyview');  
  44.             $smartyview->error ='错误的页码!';  
  45.             echo $smartyview->render('error.html');  
  46.             exit;             
  47.         }  
  48.         $this->pageid =$pageid;  
  49.         $this->prevpage = $pageid -1;  
  50.         $this->nextpage = $pageid +1;  
  51.         if ($pageid == 1) {   
  52.             $this->pagebegin = 1;          
  53.         }  
  54.         else{  
  55.             $this->pagebegin  = $pageid - floor($this->pagestep/2);  
  56.         }  
  57.         if($this->pagebegin < 1){  
  58.             $this->pagebegin = 1;  
  59.         }  
  60.         $this->pageend = $pageid + floor($this->pagestep/2);  
  61.         if ($this->pageend<$this->pagestep){  
  62.             $this->pageend = $this->pagestep;  
  63.         }  
  64.         if($this->pageend>$this->pages){  
  65.             $this->pageend = $this->pages;  
  66.         }  
  67.     }  
  68.     /** 
  69.      * 得到数据数组 
  70.      * 
  71.      * @param string $order 
  72.      * @return array 
  73.      */  
  74.     public function getArray($order=null){  
  75.         if ($this->pagesum == 0) {  
  76.              return false;  
  77.         }  
  78.         $db =$this->dbtable->getAdapter();  
  79.         $limit = $this->pagelimit;  
  80.         $offset = ($this->pageid - 1)*$this->pagelimit;  
  81.         if ($this->where == ''){  
  82.             $where = '';  
  83.         }  
  84.         else {  
  85.             $where = ' where '.$this->where;  
  86.         }  
  87.         if ($order == null) {  
  88.             $rs = $db->fetchAll('select * from '.$this->dbtable->getTable().$where.' limit '.$limit.' offset '.$offset);  
  89.         }  
  90.         else {  
  91.             $rs = $db->fetchAll('select * from '.$this->dbtable->getTable().$where.' order by '.$order.' limit '.$limit.' offset '.$offset);  
  92.         }  
  93.           
  94.         return $rs;  
  95.     }  
  96.       
  97.     /** 
  98.      * 获得page.html 
  99.      * 
  100.      * @return string 
  101.      */  
  102.     public function getResult(){  
  103.         if ($this->pagesum == 0) {  
  104.              return false;  
  105.         }  
  106.         $smartyview = Zend_Registry::get('smartyview');  
  107.         $smartyview->pageid = $this->pageid ;  
  108.         $smartyview->pages  =$this->pages;  
  109.         $smartyview->prevpage=$this->prevpage;  
  110.         $smartyview->nextpage=$this->nextpage;  
  111.         $smartyview->pagebegin =$this->pagebegin;  
  112.         $smartyview->pageend =$this->pageend;  
  113.         $smartyview->pagesum = $this->pagesum;  
  114.         $smartyview->urlparam = $this->urlparam;  
  115.         return  $smartyview->render('page.html');              
  116.     }  
  117.       
  118.     /** 
  119.      * 获得<{$page}> 
  120.      * 
  121.      */  
  122.     public function getPage(){  
  123.         $smartyview = Zend_Registry::get('smartyview');  
  124.         $smartyview->page=$this->getResult();  
  125.     }  
  126.       
  127. }  

view中的调用例子(smarty):

  1. <div id="page" align="left">  
  2. <a href="<{$siteUrl}>/page/1<{$urlparam}>/username/<{$blogname}>">头页</a>  
  3. <{if $pageid!=1}>  
  4. <a href="<{$siteUrl}>/page/<{$prevpage}><{$urlparam}>/username/<{$blogname}>">上一页</a>  
  5. <{/if}>  
  6. <{section loop=$pageend+1 name=loop  start = $pagebegin }>  
  7. <{if $smarty.section.loop.index == $pageid }>  
  8. <a href="<{$siteUrl}>/page/<{$smarty.section.loop.index}><{$urlparam}>/username/<{$blogname}>" id ="pageid"><{$smarty.section.loop.index}></a>  
  9. <{else}>  
  10. <a href="<{$siteUrl}>/page/<{$smarty.section.loop.index}><{$urlparam}>/username/<{$blogname}>"><{$smarty.section.loop.index}></a>  
  11. <{/if}>  
  12. <{/section}>  
  13. <{if $pageid != $pages}>  
  14. <a href="<{$siteUrl}>/page/<{$nextpage}><{$urlparam}>/username/<{$blogname}>">下一页</a>  
  15. <{/if}>  
  16. <a href="<{$siteUrl}>/page/<{$pages}><{$urlparam}>/username/<{$blogname}>">尾页</a>  
  17. <span>转到</span>  
  18. <select onchange="window.location.href='<{$siteUrl}>/page/'+this.value+'<{$urlparam}>/username/<{$blogname}>'">  
  19. <{section loop=$pages+1 name=loop  start = 1 }>  
  20. <{if $smarty.section.loop.index == $pageid}>  
  21. <option value="<{$smarty.section.loop.index}>" selected ><{$smarty.section.loop.index}></option>  
  22. <{else}>  
  23. <option value="<{$smarty.section.loop.index}>" ><{$smarty.section.loop.index}></option>  
  24. <{/if}>  
  25. <{/section}>  
  26. </select>  
  27. <span></span>  
  28. <span>共有数据<b> <{$pagesum}> </b>条,当前第<b> <{$pageid}> </b>页,总共有<b> <{$pages}> </b></span>  
  29. </div>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值