免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 961 | 回复: 0
打印 上一主题 下一主题

zend framework分页的实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-28 03:17 |只看该作者 |倒序浏览
ZF本身没有提供分页功能,之前我也遇到过同样的问题,看了看PEAR的Pager,也不是我想要的,急用之下就自己写了一个,结合Zend_Db_Select使用,
[复制PHP代码]

[ - ]
PHP代码如下:
/**
* @author Charles
* Sep 14, 2007 10:27:35 AM
*/
class Fidy_Pager{
public $is_empty = false;
public $_amountPerPage = 10;
public $_totalItemAmount = 0;
public $_totalPageAmount = 0;
public $_currentPageNo = 1;
public $_url = "/";
public static $_identifier = 'pageId';
public $_data = null;
public $_nextPageNo = null;
public $_previousPageNo = null;
public $_firstPageNo = null;
public $_lastPageNo = null;
/**
  * @var Zend_Db_Select
  */
public $_select = null;
public function __construct(){
  
}
/**
  * @return Fidy_Pager
  */
public function generate(){
  $anotherSql = clone $this->_select;
  $this->_totalItemAmount = $anotherSql->reset("columns")->from(null, "count(*) AS TOTAL")->query()->fetchColumn(0);
  $this->_totalPageAmount = (int) ceil($this->_totalItemAmount / $this->_amountPerPage);
  
  if($this->_totalPageAmount == 0){
   $this->is_empty = true;
   return $this;
  }
  
  if($this->_currentPageNo > $this->_totalPageAmount){
   $this->_currentPageNo = $this->_totalPageAmount;
  }
  
  $this->_firstPageNo = 1;
  $this->_lastPageNo = $this->_totalPageAmount;
  $this->_nextPageNo = ($this->_currentPageNo + 1) > $this->_lastPageNo ? $this->_lastPageNo : ($this->_currentPageNo + 1);
  $this->_previousPageNo = ($this->_currentPageNo - 1) $this->_firstPageNo ? $this->_firstPageNo : ($this->_currentPageNo - 1);
  
  $offset = ($this->_currentPageNo - 1) * $this->_amountPerPage;
  
  $this->_data = $this->_select->limit($this->_amountPerPage, $offset)->query()->fetchAll();
  
  return $this;
}
public function hasNext()
{
  return $this->_currentPageNo $this->_lastPageNo;
}
public function hasPrevious()
{
  return $this->_currentPageNo > $this->_firstPageNo;
}
/**
  * 设定sql
  *
  * @param Zend_Db_Select $sql
  * @return Fidy_Pager
  */
public function setSql(Zend_Db_Select $sql)
{
  $this->_select = $sql;
  return $this;
}
/**
  * 设定链接url
  *
  * @param string $url
  * @return Fidy_Pager
  */
public function setUrl($url="/")
{
  $this->_url = $url;
  return $this;
}
/**
  * 设定每页显示的记录数
  *
  * @param integer $num
  * @return Fidy_Pager
  */
public function setAmountPerPage($num)
{
  $amount = (int) $num;
  if($amount 1){
   $amount = 1;
  }
  
  $this->_amountPerPage = $amount;
  return $this;
}
/**
  * 设定当前页号
  *
  * @param integer $num
  * @return Fidy_Pager
  */
public function setCurrentPageNo($num)
{
  $amount = (int) $num;
  if($amount 1){
   $amount = 1;
  }
  
  $this->_currentPageNo = $amount;
  return $this;
}
public function getUrl()
{
  return htmlspecialchars($this->_url);
}
public function isEmpty()
{
  return $this->is_empty;
}
public function getAmountPerPage()
{
  return $this->_amountPerPage;
}
public function getCurrentPageNo()
{
  return $this->_currentPageNo;
}
public function getTotalItemAmount()
{
  return $this->_totalItemAmount;
}
public function getTotalPageAmount()
{
  return $this->_totalPageAmount;
}
public function getFirstPageNo()
{
  return $this->_firstPageNo;
}
public function getLastPageNo()
{
  return $this->_lastPageNo;
}
public function getNextPageNo()
{
  return $this->_nextPageNo;
}
public function getPreviousPageNo()
{
  return $this->_previousPageNo;
}
public function getData()
{
  return $this->_data;
}
public static function getIdentifier()
{
  return self::$_identifier;
}
}
?>
使用方法举例:
[复制PHP代码]

[ - ]
PHP代码如下:
$news= new News(); // 'News' 继承自 'Zend_Db_Table',当然,你也可以直接使用Zend_Db_Select
//然后拼装SQL
$sql = $news->getAdapter()
                   ->select()
                   ->from($news->getTableName()) //getTableName()这个方法是我自己加的
                   ->where("`open`='T'")
                   ->order("id DESC");
$Pager = new Fidy_Pager();
$Pager->setAmountPerPage(10)
          ->setCurrentPageNo(1)
          ->setSql($sql) //把拼装好的sql(一个Zend_Db_Select对象)传递进来,剩下的事就不用管了
          ->generate();


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/16928/showart_409169.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP