您的位置:首页 > 编程语言 > PHP开发

PHP分页类

2008-08-23 21:32 197 查看
<?php
/**
* @author Genshing
* @desc 分页处理文件
* @lastmodify 2008-8-23 17:07
*
* @param $style:二维数组,存储输出的分页样式。其中:
* count:输出当前页码和页码总数,当前页码为NOW,页码总数为COUNT EG: NOW/COUNT 则输出:1/2
* back:上一页,链接处变量为LINK
* link:普通页码链接,链接处变量为LINK,页码为PAGE
* now:当前页码,页码为PAGE
* next:下一页,链接处变量为LINK
*/
class page {
public $count;    //记录总数
public $nowpage;    //目前页码
public $paginal;    //每页记录数
public $pagenum;    //页码数量
public $pagecount;    //页码总数
public $styletype;    //分页风格选择
public $style = array(
array(
'count'=>'',
'back'=>'<a class="Arrow" href="LINK" mce_href="LINK"><<</a>',
'link'=>'<a class="SquareLink" href="LINK" mce_href="LINK">PAGE</a>',
'now'=>'<b>PAGE</b>',
'next'=>'<a class="Arrow" href="LINK" mce_href="LINK">>></a>'
),
);    //分页风格设置
public function __construct($count, $paginal=20, $pagenum=10, $styletype=0){
$this->count = intval($count);
$this->paginal = intval($paginal);
$this->styletype = intval($styletype);
if(!intval($_GET['page'])){
$this->nowpage = intval($_GET['page']) + 1;
} else {
$this->nowpage = intval($_GET['page']);
}
$this->pagenum = intval($pagenum);
$this->pagecount = intval($this->count / $this->paginal);
if($this->pagecount < 1)
$this->pagecount++;
if($this->nowpage > $this->pagecount)
$this->nowpage = 1;
}
/**
* @author Genshing
* @abstract 返回Limit变量
*/
public function getlimit(){
$ofset = $this->paginal * ($this->nowpage - 1);
return array('limit'=>$this->paginal, 'ofset'=>$ofset);
}

/**
* @author Genshing
* @abstract 获取分页输出字符串
* @return Strings
*/
public function Getlink(){
$getvar = $this->Getvar();
$link = '';
$begin = intval($this->nowpage / $this->pagenum) + 1;
if($this->pagecount <= $this->nowpage){
$end = $this->nowpage;
} elseif($this->pagecount <= ($begin + $this->pagenum - 1)) {
$end  = $this->pagecount;
} else {
$end  = $begin + $this->pagenum - 1;
}
if($this->style[$this->styletype]['count']){
$str = str_replace('NOW',$this->nowpage,$this->style[$this->styletype]['count']);
$str = str_replace('COUNT',$this->count,$str);
$link .= $str;
}
if($this->style[$this->styletype]['back']){
$link .= str_replace('LINK','?'.$getvar.'page='.($this->nowpage - 1),$this->style[$this->styletype]['back']);
}
for ($i = $begin; $i <= $end; $i++){
if($i == $this->nowpage){
if($this->style[$this->styletype]['now']){
$link .= str_replace('PAGE',$i,$this->style[$this->styletype]['now']);
}
} else {
if($this->style[$this->styletype]['link']){
$str = str_replace('LINK','?'.$getvar.'page='.$i,$this->style[$this->styletype]['link']);
$str = str_replace('PAGE',$i,$str);
$link .= $str;
}
}
}
if($this->style[$this->styletype]['next']){
$link .= str_replace('LINK','?'.$getvar.'page='.($this->nowpage + 1),$this->style[$this->styletype]['next']);
}
//print_r($_GET);
return $link;
}
/**
* @author Genshing
* @abstract 获取当前GET参数
*/
private function Getvar(){
$link = '';
foreach ($_GET as $key=>$tmp){
if($key != 'page')
$link .= $key.'='.$tmp.'&';
}
return $link;
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: