<?php
/**
* 文件名: page.class.php
* 说明: 分页类
* 作者: 江 南
* 联系方式:email:[email protected]
*
* 创建日期:2009-1-7
*/
class page {
var $perpage= 10; //每页显示多少条记录数
var $total; //总记录数
var $curpage = 1; //当前页码
var $pages; //总页数
var $offset; //记录偏移量
var $filename; //分页文件名称
var $pagename = 'page'; //分页时用来传递参数的变量 如:"$filename?page=$curpage"
var $keywords;
function page($total,$perpage,$filename,$pagename,$keys) {
$this->total = intval($total);
if(isset($perpage)) $this->perpage = $perpage;
$this->pages = ceil($this->total / $this->perpage);
$this->getcurpage();
$this->offset = ($this->curpage-1)*$this->perpage;
if(isset($pagename)) $this->pagename = $pagename;
if(isset($filename)) {
$this->filename = $filename;
} else {
$this->filename = $_SERVER['PHP_SELF'];
}
if(isset($keys)){
$this->keywords = $keys;
}else{
$this->keywords = "";
}
}
function getcurpage() {
if(isset($_GET[$this->pagename])) $this->curpage = intval($_GET[$this->pagename]);
if($this->curpage < 1) $this->curpage = 1;
if($this->curpage > $this->pages) $this->curpage = $this->pages;
}
function getfirstpage() {
if($this->curpage == 1) {
return "首页";
} else {
return "<a href=/"{$this->getlink(1)}/">首页</a>";
}
}
function getnextpage() {
$nextpage = $this->curpage + 1;
return "<a href=/"{$this->getlink($nextpage)}/">下一页</a>";
}
function getprepage() {
$prepage = $this->curpage - 1;
return "<a href=/"{$this->getlink($prepage)}/">上一页</a>";
}
function getlastpage() {
if($this->curpage == $this->pages) {
return "尾页";
} else {
return "<a href=/"{$this->getlink($this->pages)}/">尾页</a>";
}
}
function getlink($param) {
if(strpos($this->filename,'?')) {
return $this->filename."&".$this->pagename."=".$param."&".key_words."=".$this->keywords;
} else {
return $this->filename."?".$this->pagename."=".$param."&".key_words."=".$this->keywords;
}
}
function showpage() {
$begin = $this->curpage - 4;
$end = $this->curpage + 5;
if($this->curpage<=4){
$begin = 1;
$end = 10;
}
if($this->curpage > $this->pages-10){
$begin = $this->pages - 10;
$end = $this->pages;
}
if($begin<1){
$begin = 1;
}
if($end>$this->pages){
$end = $this->pages;
}
if($this->total>$this->perpage) {
$pagenav = $this->getfirstpage();
if($this->curpage > 1) {
$pagenav .= $this->getprepage();
}
for($i = $begin; $i <= $end; $i++) {
if($i == $this->curpage) {
$pagenav .= "$i"." ";
} else {
$pagenav .= "<a href=/"{$this->getlink($i)}/">[$i]</a>"." ";
}
}
if($this->curpage < $this->pages) {
$pagenav .= $this->getnextpage();
$pagenav .= $this->getlastpage();
}
}
return $pagenav;
}
}
?>