- 论坛徽章:
- 0
|
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 |
|