init(1000, 20);
* $page->setnotactivetemplate(' {a} ');
* $page->setactivetemplate('{a}');
* echo $page->show();
*
*
* @author 风居住的地方
*/
class page {
/**
* 总条数
*/
private $total;
/**
* 每页大小
*/
private $pagesize;
/**
* 总页数
*/
private $pagenum;
/**
* 当前页
*/
private $page;
/**
* 地址
*/
private $uri;
/**
* 分页变量
*/
private $pageparam;
/**
* limit xx,xx
*/
private $limit;
/**
* 数字分页显示
*/
private $listnum = 8;
/**
* 分页显示模板
* 可用变量参数
* {total} 总数据条数
* {pagesize} 每页显示条数
* {start} 本页开始条数
* {end} 本页结束条数
* {pagenum} 共有多少页
* {frist} 首页
* {pre} 上一页
* {next} 下一页
* {last} 尾页
* {list} 数字分页
* {goto} 跳转按钮
*/
private $template = '共有{total}条数据每页显示{pagesize}条数据,本页{start}-{end}条数据共有{pagenum}页{frist}{pre}{list}{next}{last}{goto}';
/**
* 当前选中的分页链接模板
*/
private $activetemplate = '{text}';
/**
* 未选中的分页链接模板
*/
private $notactivetemplate = '{text}';
/**
* 显示文本设置
*/
private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');
/**
* 初始化
* @param type $total 总条数
* @param type $pagesize 每页大小
* @param type $param url附加参数
* @param type $pageparam 分页变量
*/
public function init($total, $pagesize, $param = '', $pageparam = 'page') {
$this->total = intval($total);
$this->pagesize = intval($pagesize);
$this->pageparam = $pageparam;
$this->uri = $this->geturi($param);
$this->pagenum = ceil($this->total / $this->pagesize);
$this->page = $this->setpage();
$this->limit = $this->setlimit();
}
/**
* 设置分页模板
* @param type $template 模板配置
*/
public function settemplate($template) {
$this->template = $template;
}
/**
* 设置选中分页模板
* @param type $activetemplate 模板配置
*/
public function setactivetemplate($activetemplate) {
$this->activetemplate = $activetemplate;
}
/**
* 设置未选中分页模板
* @param type $notactivetemplate 模板配置
*/
public function setnotactivetemplate($notactivetemplate) {
$this->notactivetemplate = $notactivetemplate;
}
/**
* 返回分页
* @return type
*/
public function show() {
return str_ireplace(array(
'{total}',
'{pagesize}',
'{start}',
'{end}',
'{pagenum}',
'{frist}',
'{pre}',
'{next}',
'{last}',
'{list}',
'{goto}',
), array(
$this->total,
$this->setpagesize(),
$this->star(),
$this->end(),
$this->pagenum,
$this->frist(),
$this->prev(),
$this->next(),
$this->last(),
$this->pagelist(),
$this->gopage(),
), $this->template);
}
/**
* 获取limit起始数
* @return type
*/
public function getoffset() {
return ($this->page - 1) * $this->pagesize;
}
/**
* 设置limit
* @return type
*/
private function setlimit() {
return "limit " . ($this->page - 1) * $this->pagesize . ",{$this->pagesize}";
}
/**
* 获取limit
* @param type $args
* @return type
*/
public function __get($args) {
if ($args == "limit") {
return $this->limit;
} else {
return null;
}
}
/**
* 初始化当前页
* @return int
*/
private function setpage() {
if (!empty($_get[$this->pageparam])) {
if ($_get[$this->pageparam] > 0) {
if ($_get[$this->pageparam] > $this->pagenum)
return $this->pagenum;
else
return $_get[$this->pageparam];
}
}
return 1;
}
/**
* 初始化url
* @param type $param
* @return string
*/
private function geturi($param) {
$url = $_server['request_uri'] . (strpos($_server['request_uri'], "?") ? "" : "?") . $param;
$parse = parse_url($url);
if (isset($parse["query"])) {
parse_str($parse["query"], $params);
unset($params["page"]);
$url = $parse["path"] . "?" . http_build_query($params);
return $url;
} else {
return $url;
}
}
/**
* 本页开始条数
* @return int
*/
private function star() {
if ($this->total == 0) {
return 0;
} else {
return ($this->page - 1) * $this->pagesize + 1;
}
}
/**
* 本页结束条数
* @return type
*/
private function end() {
return min($this->page * $this->pagesize, $this->total);
}
/**
* 设置当前页大小
* @return type
*/
private function setpagesize() {
return $this->end() - $this->star() + 1;
}
/**
* 首页
* @return type
*/
private function frist() {
$html = '';
if ($this->page == 1) {
$html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true);
} else {
$html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false);
}
return $html;
}
/**
* 上一页
* @return type
*/
private function prev() {
$html = '';
if ($this->page > 1) {
$html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);
} else {
$html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);
}
return $html;
}
/**
* 分页数字列表
* @return type
*/
private function pagelist() {
$linkpage = "";
$lastlist = floor($this->listnum / 2);
for ($i = $lastlist; $i >= 1; $i--) {
$page = $this->page - $i;
if ($page >= 1) {
$linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
} else {
continue;
}
}
$linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);
for ($i = 1; $i page + $i;
if ($page pagenum) {
$linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
} else {
break;
}
}
return $linkpage;
}
/**
* 下一页
* @return type
*/
private function next() {
$html = '';
if ($this->page < $this->pagenum) {
$html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false);
} else {
$html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true);
}
return $html;
}
/**
* 最后一页
* @return type
*/
private function last() {
$html = '';
if ($this->page == $this->pagenum) {
$html .= $this->replace($this->uri.'&page='.($this->pagenum), $this->config['last'], true);
} else {
$html .= $this->replace($this->uri.'&page='.($this->pagenum), $this->config['last'], false);
}
return $html;
}
/**
* 跳转按钮
* @return string
*/
private function gopage() {
$html = '';
$html.=' ';
return $html;
}
/**
* 模板替换
* @param type $replace 替换内容
* @param type $result 条件
* @return type
*/
private function replace($url, $text, $result = true) {
$template = ($result ? $this->activetemplate : $this->notactivetemplate);
$html = str_replace('{url}', $url, $template);
$html = str_replace('{text}', $text, $html);
return $html;
}
}