/*
分页的CSS代码:使用要复制到相关CSS页面
#data_page {
width:100%;
color:blue;
line-height:30px;
}
#data_page span{
color:red; font-weight:bold;
}
#data_page .page_num,
#data_page .current_page{
width:12px;
padding:3px 5px;
line-height:10px;
border-left:1px solid #CCC;
border-top:1px solid #CCC;
border-right:1px solid #999;
border-bottom:1px solid #666;
margin:0px 2px;
}
#data_page .page_num:hover,
#data_page .current_page{
border:1px inset #999;
color:#333;
text-decoration:none;
}
#data_page .current_page{
background-color:#9CF;
}
分页类,
在使用此分页类之前,必须在表单保证查询串的值不为空,如:?do=xx
*/
class Page{
private $pageRecordNum = 10; //默认每页记录数为10条
private $total=0; //总条数
private $totalNum=0; //总页数
private $p = 1; //当前页
private $np = 1; //下一页
private $rp = 1; //上一页
private $page = "page=";
private $url;
private $interval_page = 10; //根据该值限定一组分页的页码数
public function setPageRecordNum($pageRecordNum){
$this->pageRecordNum = $pageRecordNum;
}
public function getPageRecordTotal(){
return $this->pageRecordNum*($this->p-1);
}
public function set($url,$total){
$this->url = $url;
$this->total = $total;
}
public function getPageRecordNum(){
return $this->pageRecordNum;
}
public function getQueryRange(){
$this->formatUrl();
return " limit ".$this->getPageRecordTotal().",".$this->pageRecordNum;
}
private function formatUrl(){ // 先提取page的值,在将其值赋予np或者rp
$total = $this->getTotalNum();
if(isset($_GET['page']))
$this->p = $_GET['page'];
if($total > 1){
if($this->p-1 < 1){
$this->rp = 1;
}else{
$this->rp = $this->p-1;
}
if($this->p+1 > $total){
$this->np = $total;
}else{
$this->np = $this->p+1;
}
$replace_url = parse_url($this->url);
if(isset($replace_url["query"])){
$query_url = $replace_url["query"];
$nu = @ereg_replace("(^|&)page=$this->p","",$query_url);
$this->url = @ereg_replace($query_url,"",$this->url);
$this->url .= (!$nu) ? "" : $nu."&";
}else{
$this->page = "?".$this->page;
}
}
}
public function display(){
$total = $this->getTotalNum();
if($total > 1){
$r = $this->page.$this->rp;
$n = $this->page.$this->np;
$u = $this->url.$this->page;
$page = "<div id='data_page'>".
"总计<span>{$this->total}</span>条记录 共<span>{$total}</span>页 ";
if($this->p - $this->interval_page >= 1)
$page .= "<a href='{$this->url}{$r}' style='margin-right:5px;' class='page_num' title='上一页'><</a>";
$base = ceil($this->p / $this->interval_page);
$begin_base = $base * $this->interval_page - ($this->interval_page-1);
if($begin_base < 1)
$begin_base = 1;
$end_base = $begin_base + $this->interval_page - 1;
if($end_base > $total){
$end_base = $total;
}
for(; $begin_base<=$end_base; $begin_base++){
$className = ($begin_base == $this->p ? 'current_page' : 'page_num');
if($this->p == $begin_base){
$page .= "<span class='{$className}'>{$begin_base}</span>";
}else{
$page .= "<a href='{$u}{$begin_base}' class='{$className}'>{$begin_base}</a>";
}
}
if($base * $this->interval_page < $total)
$page .= "<a href='{$this->url}{$n}' style='margin-left:5px;' class='page_num' title='下一页'>></a>";
$page .= "</div>";
$this->url = $this->url.$this->page.$this->p;
}else{
$page = "<div style='width:100%;color:blue; line-height:30px;'>总计<span style='color:red;font-weight:bold;'> {$this->total} </span>条记录</div>";
}
return $page;
}
private function countRecord(){
$p = $this->p == 1 ? 0 : $this->p;
return $this->p*$this->pageRecordNum-($this->pageRecordNum-1);
}
public function getTotalNum(){
$page = ceil($this->total/$this->pageRecordNum);
return (int)$page;
}
public function getURL(){
return $this->url;
}
//用于在分页时生成get查询参数
//$arr 分页时根据该值生成相应的url参数 k=>v形式
public function str_link($arr=array()){
$url = parse_url($_SERVER['REQUEST_URI']);
$query = isset($url["query"]) ? $url["query"] : "?";
$param = "";
if($query != "?"){
$_SERVER['REQUEST_URI'] = str_replace($query,"",$_SERVER['REQUEST_URI']);
}else{
$_SERVER['REQUEST_URI'] .= "?";
}
foreach($arr AS $key=>$value){
if(!empty($value)){
$value = urlencode($value);
$param .= "$key=$value&";
}
}
$query = rtrim($param,"&");
if(!preg_match("/".substr($query,1,strlen($query))."/",$_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] .= $query;
}
return "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
}