全部博文(921)
分类:
2007-10-28 03:17:42
/**
* @author Charles <[email=fidy.watcher@gmail.com]fidy.watcher@gmail.com[/email]>
* 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;
}
}
?>
chinaunix网友2008-09-11 12:15:55
饿~~这个...貌似应用到group by后就没办法了~~ 我现在用了个更简单的方法~~比如做个方法 public function countTotal($select) // Zend_Db_Select { $select->limit(); $sqlString = "Select Count(*) From ({$select->__toString()}) As ARC"; $result = $this->getAdapter()->fetchOne($sqlString); return (int)$result; }