一个简单且很好用的php分页类
class page {
// 分页栏每页显示的页数
public $rollpage = 6;
// 页数跳转时要带的参数
public $parameter ;
// 默认列表每页显示行数
public $listrows = 20;
// 起始行数
public $firstrow ;
// 分页总页面数
protected $totalpages ;
// 总行数
protected $totalrows ;
// 当前页数
protected $nowpage ;
// 分页的栏的总页数
protected $coolpages ;
// 分页显示定制
protected $config = array(
'redirect'=>false,
'header'=>'条记录',
'prev'=>'上一页',
'next'=>'下一页',
'first'=>'1',
'last'=>'最后一页',
'theme'=>' <div class="part1">%uppage% %first% %prepage% %linkpage% %nextpage% %downpage%</div> <div class="part2">共 %totalpage% 页');
// 默认分页变量名
protected $varpage;
/**
+----------------------------------------------------------
* 架构函数
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $totalrows 总的记录数
* @param array $listrows 每页显示记录数
* @param array $parameter 分页跳转的参数
+----------------------------------------------------------
*/
public function __construct($totalrows,$listrows='',$parameter='') {
$this->totalrows = $totalrows;
$this->parameter = $parameter;
$this->varpage = c('var_page') ? c('var_page') : 'p' ;
if(!empty($listrows)) {
$this->listrows = intval($listrows);
}
$this->totalpages = ceil($this->totalrows/$this->listrows); //总页数
$this->coolpages = ceil($this->totalpages/$this->rollpage);
//$_get验证
$this->nowpage = intval($_get[$this->varpage]);
$this->nowpage = $this->nowpage > 0 ? $this->nowpage : 1;
if(!empty($this->totalpages) && $this->nowpage>$this->totalpages) {
$this->nowpage = $this->totalpages;
}
$this->firstrow = $this->listrows*($this->nowpage-1);
}
public function setconfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
+----------------------------------------------------------
* 分页显示输出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function show() {
if(0 == $this->totalrows) return '';
//处理参数
$p = $this->varpage;
$url = $_server['request_uri'].(strpos($_server['request_uri'],'?')?'':"?").$this->parameter;
$parse = parse_url($url);
if(isset($parse['query'])) {
parse_str($parse['query'],$params);
unset($params[$p]);
$url = $parse['path'].'?'.http_build_query($params);
}
/* 分页逻辑 */
//当总数小于显示的页码数
if ($this->totalpages <= $this->rollpage) {
$start = 1;
$end = $this->totalpages;
}
else{
//
if ($this->nowpage <= $this->rollpage - 1) {
$start = 1;
$end = $this->rollpage;
$islast = true;
}
else if ($this->nowpage > $this->totalpages - $this->rollpage + 1) {
$start = $this->totalpages - ($this->rollpage - 1);
$end = $this->totalpages;
$isfirst = true;
}
else{
//浮动数
$size = floor($this->rollpage / 2);
$start = $this->nowpage - $size;
$end = $this->nowpage + $size;
$isfirst = true;
$islast = true;
}
}
//上下翻页字符串
$uprow = $this->nowpage - 1;
$downrow = $this->nowpage + 1;
/* 拼装html */
//< 1... ...last >
if ($isfirst){
$thefirst = "<a class='firstpage' href='".$url."&".$p."=1' >".$this->config['first']."</a>";
}
if ($islast){
$theend = "<a class='lastpage' href='".$url."&".$p."=$this->totalpages' >".$this->config['last']."</a>";
}
if ($uprow > 0){
$uppage = "<a class='uppage' href='".$url."&".$p."=$uprow'>".$this->config['prev']."</a>";
}
if ($downrow <= $this->totalpages){
$downpage = "<a class='downpage' href='".$url."&".$p."=$downrow'>".$this->config['next']."</a>";
}
if($start==3){
$linkpage .= "<a href='".$url."&".$p."=2'>2</a>";
}
if($start>=4){
$linkpage .= "<a href='".$url."&".$p."=2'>2</a> <span class='noendclass'>...</span>";
}
//1 2 3 4 5
for($i=$start;$i<=$end;$i++){
if($i!=$this->nowpage){
$linkpage .= " <a href='".$url."&".$p."=$i'> ".$i." </a>";
}else{
$linkpage .= " <span class='current'>".$i."</span>";
}
if($i==$end){
if($i<$this->totalrows){
$linkpage .= " <span class='noendclass'>...</span>";
}
}
}
$pagestr = str_replace(
array('%header%','%nowpage%','%totalrow%','%totalpage%','%uppage%','%first%','%prepage%','%linkpage%','%nextpage%','%downpage%','%end%'),
array($this->config['header'],$this->nowpage,$this->totalrows,$this->totalpages,$uppage,$thefirst,$prepage,$linkpage,$nextpage,$downpage,$theend),$this->config['theme']);
//显示模式 普通false 带跳转ture
if (!empty($this->config['redirect'])){
$html = $pagestr;
}else{
//传递参数
if($this->totalpages > 1){
$redirect = " 到第 <form method='get' action=''><input name=".$p." type='text' class='page_text' size='3' maxlength='3' value='" . $this->nowpage ."'/> 页 <input type='submit' class='page_btn' value='确定' />";
if ($params){
foreach($params as $k => $v){
$string .= "<input type='hidden' name='" . $k . "' value='" . $v . "'/>";
}
$redirect = $redirect . $string . '</form></div>';
}else{
$redirect = $redirect . '</form></div>';
}
}
//生成html字符串
$html = $pagestr . $redirect;
}
return $html;
}
}
上一篇: 你应该知道的简单易用的CSS技巧
下一篇: php登陆页的密码处理方式分享