首页 - 标签 - 登陆 | emlog - 我们的动漫
文字大小:- +

基于Zend Framework的分页类

2008-7-10 21:41 Thursday
作者:朦朧中的罪惡 | 标签: php 原创 Zend Framework

刚学Zend Framework 感叹其强大的同时却发现其在Web开发中常用到的如分页,上传,字符串处理等等却没有实现,今天小试牛刀,给Zend Framework扩展了一个分页类。

核心编写思路:

     本分页类未采用常见的将HTML代码生成封装在类中的做法,而是基于Zend框架的MVC的思想,只在类中封装了处理分页所必需的数据的逻辑,而具体的分页信息显示逻辑则交予视图完成,将业务逻辑与显示逻辑分离,这样在开发灵活性上可以提高很多。

本例基于Zend Framework  1.5.2 编写,未将类整合到Zend Framework中,将本文件放置于INCLUDE_PATH/Util/下即可。本类只能在控制器下的方法中使用,实例化时需要传入控制器的request对象。

类核心代码:Page.php

  1. <?php  
  2. /**  
  3.  * 基于Zend Framework分页类  
  4.  *    
  5.  * 作者:朦朧中的罪惡  
  6.  * 博客: http://be-evil.org  
  7.  *  
  8.  * @package    Util  
  9.  */ 
  10. class Util_Page  
  11. {  
  12.     /**  
  13.      * 数据总数  
  14.      * @var int  
  15.      */ 
  16.     protected $_total;  
  17.     /**  
  18.      * 当前页码  
  19.      * @var int  
  20.      */ 
  21.     protected $_curpage;  
  22.     /**  
  23.      * 每页记录数  
  24.      * @var int  
  25.      */ 
  26.     protected $_perpage;  
  27.     /**  
  28.      * 分页参数名称  
  29.      * @var string  
  30.      */ 
  31.     protected $_pagename;  
  32.     /**  
  33.      * Zend_Controller_Request_Http对象  
  34.      * @var object  
  35.      */ 
  36.     protected $_request;  
  37.       
  38.       
  39.     /**  
  40.      * 类构造函数  
  41.      *  
  42.      * 初始化类内部属性  
  43.      *   
  44.      * @param int $total 总数  
  45.      * @param int $curpage 当前页码  
  46.      * @param int $perpage 每页记录数  
  47.      * @return void  
  48.      */ 
  49.     public function __construct(Zend_Controller_Request_Http $request,$total,$perpage = 10,$pagename = 'page')  
  50.     {  
  51.         $this->_total   = intval($total);  
  52.         $this->_perpage = 10;  
  53.         $this->_pagename = $pagename;  
  54.         $this->_request  = $request;  
  55.         $this->_curpage = intval($this->_request->getParam($pagename));  
  56.     }  
  57.       
  58.     /**  
  59.      *  
  60.      * 根据已经赋值的类属性计算相关参数  
  61.      *   
  62.      * @return array  
  63.      */ 
  64.     public function calculatePage()  
  65.     {  
  66.         $pageArray = array();  
  67.         $pageArray['pagename']  = $this->_pagename;  
  68.         $pageArray['total']     = $this->_total;  
  69.         //算出总页数  
  70.         $pageArray['totalpage'] = (int) ceil($this->_total/$this->_perpage);  
  71.         $pageArray['pagestart'] = 0;  
  72.           
  73.         //算出数据开始的行数  
  74.         if($this->_curpage < 1)  
  75.         {  
  76.             $this->_curpage = 1;  
  77.         }  
  78.         else 
  79.         {  
  80.             $this->_curpage > $pageArray['total'] && $this->_curpage = $pageArray['total'];  
  81.             $pageArray['pagestart'] = ($this->_curpage - 1) * $this->_perpage;   
  82.         }  
  83.  
  84.         $pageArray['curpage']  = $this->_curpage;  
  85.         $pageArray['perpage']  = $this->_perpage;  
  86.           
  87.         //通过传入的Zend_Controller_Request_Http类获得当前控制器的相关信息  
  88.         $moduleName     = $this->_request->getModuleName();  
  89.         $controllerName = $this->_request->getControllerName();  
  90.         $actionName     = $this->_request->getActionName();  
  91.         //获得参数  
  92.         $params         = $this->_request->getParams();  
  93.           
  94.         //初始化基本的链接  
  95.         $pageArray['url'] =  '/'.$moduleName . '/' . $controllerName . '/' . $actionName . '/';  
  96.           
  97.         if($params && is_array($params))  
  98.         {  
  99.             //反转数组的键与值  
  100.             $params = array_flip($params);  
  101.             //过滤参数中的页码参数和值  
  102.             $params = array_filter($params,array($this,'_filterPage'));  
  103.             //再次反转数组的键与值  
  104.             $params = array_flip($params);  
  105.               
  106.             //循环生成参数链接  
  107.             foreach($params as $key => $value)  
  108.             {  
  109.                 $pageArray['url'] .= $key . '/' . $value . '/';  
  110.             }  
  111.         }  
  112.         return $pageArray;  
  113.     }  
  114.       
  115.     /**  
  116.      *  
  117.      * 清空类属性的赋值  
  118.      *   
  119.      * @return array  
  120.      */ 
  121.     public function cleanUp()  
  122.     {  
  123.         $this->_curpage = $this->_perpage = $this->_curpage = 0;  
  124.         $this->_request = null;  
  125.     }  
  126.       
  127.     /**  
  128.      * 设定内部属性$total的值  
  129.      *  
  130.      *   
  131.      * @return void  
  132.      */ 
  133.     public function setTotal($total)  
  134.     {  
  135.         $this->_total = intval($total);  
  136.     }  
  137.       
  138.     /**  
  139.      * 设定内部属性$curpage的值  
  140.      *  
  141.      *   
  142.      * @return void  
  143.      */ 
  144.     public function setCurpage($curpage)  
  145.     {  
  146.         $this->_curpage = intval($curpage);  
  147.     }  
  148.       
  149.     /**  
  150.      * 设定内部属性$perpage的值  
  151.      *  
  152.      *   
  153.      * @return void  
  154.      */ 
  155.     public function setPerpage($perpage)  
  156.     {  
  157.         $this->_perpage = intval($perpage);  
  158.     }  
  159.       
  160.     /**  
  161.      * 设定内部属性$perpage的值  
  162.      *  
  163.      *   
  164.      * @return void  
  165.      */ 
  166.     public function setPagename($pagename)  
  167.     {  
  168.         $this->_pagename = $pagename;  
  169.     }  
  170.       
  171.     /**  
  172.      * 设定内部对象$request  
  173.      *  
  174.      *   
  175.      * @return void  
  176.      */ 
  177.     public function setRequest(Zend_Controller_Request_Http $request)  
  178.     {  
  179.         $this->_request = $request;  
  180.     }  
  181.  
  182.     /**  
  183.      * 过滤Zend_Controller_Request_Http对象参数中的模块、控制器、动作及分页标识参数  
  184.      *  
  185.      *   
  186.      * @return bool  
  187.      */ 
  188.     protected function _filterPage($paramname)  
  189.     {  
  190.         $filter = array('module','action','controller',$this->_pagename);  
  191.         return (in_array($paramname,$filter) ? false : true);  
  192.     }  

 在控制器中的类的使用:

 注意数据游标的偏移量已经在类中算好,调用数量则是分页类的每页条数属性

  1.   public function indexAction()  
  2.   {  
  3. //获得日志总数  
  4.     $blogcount = $this->_blog->getBlogCount();  
  5. //实例化分类页对象  
  6.     $page  = new Util_Page($this->_request,$blogcount);  
  7. //计算相关数据  
  8.     $pages = $page->calculatePage();  
  9. //根据分页算出的数据偏移量调用数据  
  10.     $blogs =  $this->_blog->getBlogList($pages['perpage'],$pages['pagestart']);  
  11. //视图变量赋值  
  12.     $this->view->assign(array('blogs'=>$blogs,'pages'=>$pages));  
  13.   }  
  14.    
 

在视图中的分页信息显示处理:

 

  1. <?php if($this->pages['totalpage'] > 1): ?>  
  2.     <Table width="98%" align="center">  
  3.         <tr>  
  4.             <td>  
  5.             总<?php echo $this->pages['totalpage']  ?>页/<?php echo $this->pages['total']  ?>条记录 当前第<?php echo $this->pages['curpage']  ?>页  
  6.             <a href="<?php echo $this->pages['url']  ?>page/1/">首页</a>  
  7.             <?php if($this->pages['curpage'] > 1): ?>  
  8.                 <a href="<?php echo $this->pages['url']  ?>page/<?php echo $this->pages['curpage']-1  ?>/">上一页</a>  
  9.             <?php endif; ?>  
  10.             |  
  11.             <?php if($this->pages['curpage'] <= $this->pages['totalpage']): ?>  
  12.                 <a href="<?php echo $this->pages['url']  ?>page/<?php echo $this->pages['curpage']+1  ?>/">下一页</a>  
  13.             <?php endif; ?>  
  14.             <a href="<?php echo $this->pages['url']  ?>page/<?php echo $this->pages['totalpage'] ?>/">末页</a>  
  15.             </td>  
  16.         </tr>  
  17.     </Table>  
  18. <?php endif; ?> 

小结&心得

第一次用面向对象的思想写扩展,同时对Zend的机制了解还不是很透彻,可能类中的某些方法可以有更好的处理方式。在以后的学习中慢慢改进吧,最后依然对Zend没有封装网站常用业务逻辑感到费解....

转载请注明出自 http://be-evil.org 思想之地,谢谢合作。

标签: php 原创 Zend Framework | 其他文章 : « ADSL使用路由器出现FTP上传缓慢的解决办法 | 超酷Firefox扩展better Gmail 2»

  • 引用地址:

评论

暂无评论

发表评论

姓  名:
电子邮件: (选填)
个人主页: (选填)
内  容:
  记住我