<?php
/** 分页类 **/
class Application_Model_DbTable_Page{
public $up_page;
public $next_page;
public $count_page;
public $count_record;
public $prefix;
public $suffix;
public $limit;
public $current_page;
public $sql;
public $db;
public $queryWhere;
public $data;
function __construct($current_page=1,$sql="",$limit=10) {
$this->current_page = $current_page;
$this->sql = $sql;
$this->limit = $limit;
$this->db = Zend_Db_Table::getDefaultAdapter();
}
function getData(){
return $this->data;
}
function setData($data){
$this->data = $data;
}
function setLimit($limit){
$this->limit = $limit;
}
function setSql($sql){
$this->sql = $sql;
}
function setCurentPage($current_page){
$this->current_page = $current_page;
}
function getPageAttr(){
return array(
"count_record"=>$this->getCountRecord(),
"count_page"=>$this->getCountPage(),
"current_page"=>$this->getCurrentPage(),
"up_page"=>$this->getUpPage(),
"next_page"=>$this->getNextPage(),
"limit"=>$this->getLimit(),
"sql"=>$this->getSql(),
"prefix"=>$this->getPrefix(),
"data"=>$this->getData()
);
}
function getCurrentPage(){
if($this->current_page == '' || $this->current_page === null){
$this->current_page = 1;
}
if($this->current_page<=1){
$this->current_page = 1;
}
if($this->current_page>=$this->getCountPage()){
$this->current_page = $this->getCountPage();
}
return $this->current_page;
}
function getCountPage(){
return $this->count_page = ceil($this->getCountRecord()/$this->getLimit());
}
function getCountRecord(){
/** if($this->count_record == null){ 这是之前的写法。因为记录条数可能为 0,而为零的时候,导致这 0==null 每调用一次该方法时,就去查询数据库,所以引起效率底下!。*/
//
if($this->count_record === null){
$db = $this->getDB();
$sql = $this->getSql();
$result = $db->fetchAll ( $sql );
$this->count_record = count($result);
}
return $this->count_record;
}
function getDB(){
if($this->db === null){
$this->db = Zend_Db_Table::getDefaultAdapter();
}
return $this->db;
}
function getSql(){
return $this->sql;
}
function getUpPage(){
if($this->getCurrentPage() - 1 < 1){
$this->up_page = 1;
}else{
$this->up_page = $this->getCurrentPage() - 1;
}
return $this->up_page;
}
function getNextPage(){
if($this->getCurrentPage() + 1 > $this->getCountPage()){
$this->next_page = $this->getCountPage();
}else{
$this->next_page = $this->getCurrentPage() + 1;
}
return $this->next_page;
}
function getLimit(){
if($this->limit == '' || $this->limit === null){
$this->limit = 10;
}
return $this->limit;
}
function getPrefix(){
if(($this->getCurrentPage()-1)<0){
$this->prefix = 0;
}else{
$this->prefix = ($this->getCurrentPage()-1) * $this->getLimit();
}
return $this->prefix;
}
}
转载于:https://blog.51cto.com/wo1cto/599448