<?php 

//分页类
class PageC{
	
	public $Page;	//当前页
	
	public $Total;	//数据总条数
	
	public $PageSize;	//每页显示条数
	
	public $PageNum;	//总页数
	
	public $Url;	//链接
	
	//开始条数
	public function Pg(){
		return ($this->Page - 1) * $this->PageSize;
	}
	
	//判断是否是正整数
	public function check_zzs($varnum){
		$string_var = "0123456789";
		$len_string = strlen($varnum);
		if(substr($varnum,0,1)=="0"){
			return false;
			die();
		}else{
			for($i=0;$i<$len_string;$i++){
				$checkint = strpos($string_var,substr($varnum,$i,1));
				if($checkint===false){
					return false;
					die();
				}
			}
			return true;
		}
	}
	
	//计算总共有多少页
	public function GetPageNum(){
		$PageNum = $this->Total/$this->PageSize;
		if($this->check_zzs($PageNum)){
			return $PageNum;
		}
		else{
			return intval($PageNum + 1);
		}
	}
	
	//替换链接页码
	public function SetUrlPage($PageId){
		$SetUrlPage = str_replace('{#Page}', $PageId, $this->Url);
		return $SetUrlPage;
	}
	
	//前后分页连接
	public function SetPageUrl(){
		$ye = $this->GetPageNum();
		$page = $this->Page;
		$Index = $this->SetUrlPage(1);
		$Prev = $this->SetUrlPage($page-1);
		$Next = $this->SetUrlPage($page+1);
		$End = $this->SetUrlPage($ye);
		if($ye > 1){
			if($page > 1 && $page < $ye){
				$left = "<a href='".$Index."'>首页</a><a href='".$Prev."'>上一页</a>";
				$right = "<a href='".$Next."'>下一页</a><a href='".$End."'>末页</a>";
			}
			if($page == 1){
				$left = '';
				$right = "<a href='".$Next."'>下一页</a>&nbsp;<a href='".$End."'>末页</a>";
			}
			if($page == $ye){
				$left = "<a href='".$Index."'>首页</a><a href='".$Prev."'>上一页</a>";
				$right = '';
			}
		}else{
			$left = '';
			$right = '';
		}
		$pageurl['l'] = $left;
		$pageurl['r'] = $right;	
		return $pageurl;	
	}
	
	//手机样式
	public function SetMPageUrl(){
		$ye = $this->GetPageNum();
		$page = $this->Page;
		$Index = $this->SetUrlPage(1);
		$Prev = $this->SetUrlPage($page-1);
		$Next = $this->SetUrlPage($page+1);
		$End = $this->SetUrlPage($ye);
		if($ye > 1){
			if($page > 1 && $page < $ye){
				$left = "<a class='Left' href='".$Prev."'>上一页</a>";
				$right = "<a class='Right' href='".$Next."'>下一页</a>";
			}
			if($page == 1){
				$left = '';
				$right = "<a class='Right' href='".$Next."'>下一页</a>";
			}
			if($page == $ye){
				$left = "<a class='Left' href='".$Prev."'>上一页</a>";
				$right = '';
			}
		}else{
			$left = '';
			$right = '';
		}
		$pageurl['l'] = $left;
		$pageurl['r'] = $right;
		return $pageurl;
	}	
	
	//页码列表
	public function PageCodeList(){
		$ye = $this->GetPageNum();
		$page = $this->Page;
		$OnPageUrl = $this->SetUrlPage($page);		
		$page_codea = '';
		$page_codeb = '';
		if(($ye == 0) or ($ye == 1)){
			$page_on = '';
		}
		else{
			$page_on = "<a href='".$OnPageUrl."' style='font-weight:bold; color:#FF0000;'>".$page."</a>";
		}
		if(($page - 1) > 5){
			$page_a = 5;
		}
		else{
			$page_a = $page - 1;
		}
		for($i = ($page - $page_a); $i <= ($page - 1); $i++){
			$PageL = $this->SetUrlPage($i);
			$page_codea = $page_codea."<a href='".$PageL."'>".$i."</a>";
		}
		
		if(($ye - $page) > 5){
			$page_b = 5;
		}
		else{
			$page_b = $ye - $page;
		}
		for($i = ($page + 1); $i <= ($page + $page_b); $i++){
			$PageR = $this->SetUrlPage($i);
			$page_codeb = $page_codeb."<a href='".$PageR."'>".$i."</a>";
		}
		$PageCodeList['l'] = $page_codea;
		$PageCodeList['m'] = $page_on;
		$PageCodeList['r'] = $page_codeb;
		return $PageCodeList;
	}
	
	//生成分页连接
	public function CreatePageCodeList($Style = 1){
			$this->PageIdCheck();
			if($Style == 1){
				$PageUrl = $this->SetPageUrl();
				$CodeList = $this->PageCodeList();
				$PageCodeList = $PageUrl['l'].$CodeList['l'].$CodeList['m'].$CodeList['r'].$PageUrl['r'];
			}
			if($Style == 2){
				$PageUrl = $this->SetMPageUrl();
				$PageCodeList = $PageUrl['l'].$PageUrl['r'];				
			}
			return $PageCodeList;
	}
	
	//防止超出范围
	public function PageIdCheck(){
		$ye = $this->GetPageNum();
		$page = $this->Page;
		if($page < 1){  //如果当前页数小于1
			$GotoURL = $this->SetUrlPage(1);
			header("Location: $GotoURL ");
			exit();
		}
		if($ye > 1){ //如果总页数大于1
			if($page > 1){  //如果当前页也大于1
				if($page > $ye){  //如果当前页大于总页数
					$GotoURL = $this->SetUrlPage($ye);
					header("Location: $GotoURL ");
					exit();
				}
			}
		}
		if($ye < 1 || $ye == 1){ //如果总页数小于1或等于1
			if($page > 1){ //如果当前页大于1
				//则跳转到第一页
				$GotoURL = $this->SetUrlPage(1);
				header("Location: $GotoURL ");
				exit();
			}
		}		
	}
	
	
}

?>