|
发表于 2015-5-18 15:15:55
|
显示全部楼层
本帖最后由 tokyo2006 于 2015-5-18 15:18 编辑
我来贴一个我自己写的Page类吧
PHP复制代码 <?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class Page {
var $page_size = DEFAULT_PAGE_SIZE ;
var $start = DEFAULT_PAGE_START ;
var $total_count;
var $result;
var $to_string='this is page object';
function __construct () {
}
public function init ($start, $total_count, $page_size, $result)
{
$this->page_size = $page_size;
$this->result = $result;
$this->total_count = $total_count;
$this->start = $start;
return $this;
}
/**
* 取总记录数.
*/
public function getTotalCount () {
return $this->total_count;
}
/**
* 取总页数.
*/
public function getTotalPageCount () {
if ($this->total_count % $this->page_size == 0)
return intval($this->total_count / $this->page_size);
else
return intval($this->total_count / $this->page_size + 1);
}
/**
* 取每页数据容量.
*/
public function getPageSize () {
return $this->page_size;
}
/**
* 取当前页中的记录.
*/
public function getResult () {
return $this->result;
}
/**
* 取该页当前页码,页码从1开始.
*/
public function getCurrentPageNo () {
return intval($this->start / $this->page_size + 1);
}
/**
* 该页是否有下一页.
*/
public function hasNextPage () {
return ($this->getCurrentPageNo() < $this->getTotalPageCount());
}
/**
* 该页是否有上一页.
*/
public function hasPreviousPage () {
return $this->getCurrentPageNo() > 1;
}
}
?> 复制代码
使用的时候你可以先载入这个类然后使用它,请确保在autoload.php中载入了这个类,下面是一个使用page的方法
PHP复制代码 /** * search area list for page
* @param type $name
* @param type $headName
* @param type $currentPostion
* @param type $limit
* @param type $orderName
* @param type $asc
* @return type
*/
public function getAreaList($name, $headName, $currentPage, $limit, $orderName, $asc) {
$currentPostion = ($currentPage-1)*$limit;
$select = 'select id,name,depth,headName,parentId ';
$from = ' from tour_area';
$where = ' where 1=1';
if ($headName != '') {
$where = $where . ' and headName='.$headName;
}
if ($name != '') {
$where = $where . ' and name like '.'\'%'.$name.'%\'';
}
if ($asc) {
$where = $where . ' order by ' . ' ' . $orderName . ' asc';
} else {
$where = $where . ' order by ' . ' ' . $orderName . ' desc';
}
$total_count = $this->db->query('select count(0) as cnt '.$from.$where)->row()->cnt;
$where = $where . ' limit ' . $currentPostion . ',' . $limit;
$result = $this->db->query($select.$from.$where)->result();
$page = new Page();
$page->init($currentPostion, $total_count, $limit,$result);
return $page;
} 复制代码
虽然我的实现方法可能有些笨拙,但是基本上可以满足我的开发需求
|
|