class Page {
private $total;//Total quantity private $limit;//Return the limit statement of mysql private $pageStart;//Starting value private $pageStop;//Ending value private $pageNumber;//Display the number of paging numbers private $page;//Current page private $pageSize;//The number displayed on each page private $pageToatl;//Paging The total number of private $pageParam;//Paging variables private $uri;//URL parameters /** * The paging setting style is not case-sensitive * %pageToatl% //Total number of pages * %page%//Current page * %pageSize% //Number of data items displayed on the current page * %pageStart%//This page Starting number * %pageStop%//Ending number of this page * %total%//Total number of data * %first%//Homepage * %ending%//Last page * %up%/ /Previous page * %down%//Next page * %F%//Start page * %E%//End page * %omitFA%//Omit and jump before * %omitEA% //Omit after and jump * %omitF%//Omit before * %omitE%//Omit after * %numberF%//Fixed number of number pagination * %numberD%//Equal left and right number pagination * %input%//Jump input box * %GoTo%//Jump button */ bbs.it-home.org private $pageType = 'Page %page%/Total%pageToatl% %first%%up%%F%%omitFA%%numberF%%omitEA%%E%%down%%ending%';
/ /Display value setting
private $pageShow = array('first'=>'Home page','ending'=>'Last page','up'=>'Previous page','down'=> 'Next page','GoTo'=>'GO');
/**
* Initialization data, construction method
* @access public
* @param int $total The total number of data
* @param int $pageSize The number of items displayed on each page
* @param int $pageNumber The number of paging numbers displayed (use %numberF% It will have different effects than using %numberD%)
* @param string $pageParam paging variable
* @return void
*/
public function __construct($total,$pageSize=10,$pageNumber =5,$pageParam='p'){
$this->total = $total<0?0:$total; $this->pageSize = $pageSize<0?0:$pageSize; $this ->pageNumber = $pageNumber<0?0:$pageNumber; $this->pageParam = $pageParam;
$this->calculate();
}
/* *
* Show pagination
* @access public
* @return string HTML pagination string
*/
public function pageShow(){
$this->uri();
if($this->pageToatl>1){
if($this->page == 1){
$first = ''.$this->pageShow['first'].' ';
$up = ''.$this->pageShow['up'].' ';
}else{
$first = ''.$this->pageShow['first'].' ';
$up = ''.$this->pageShow['up'] .' ';
}
if($this->page >= $this->pageToatl){
$ending = ''.$this ->pageShow['ending'].' ';
$down = ''.$this->pageShow['down'].'< /span>';
}else{
$ending = ''.$this->pageShow['ending'].' ';
$down = ''.$this->pageShow['down'].' ';
}
$input = ' ';
$GoTo = ''.$this->pageShow['GoTo'].' ';
}else{
$first = ''; $up ='';$ending = '';$down = '';$input = '';$GoTo = '';
}
$this->numberF();
return str_ireplace(array('%pageToatl%','%page%','%pageSize%','%pageStart%','%pageStop%','%total%','%first%','%ending%','%up%','%down%','%input%','%GoTo%'),array($this->pageToatl,$this->page,$this->pageStop-$this->pageStart,$this->pageStart,$this->pageStop,$this->total,$first,$ending,$up,$down,$input,$GoTo),$this->pageType);
}
/**
*Number Pagination
*/
private function numberF(){
$pageF = stripos($this->pageType,'%numberF%');
$pageD = stripos($this->pageType,'%numberD%');
$numberF = '';$numberD = '';$F = '';$E ='';$omitF = '';$omitFA = '';$omitE = '';$omitEA = '';
if($pageF!==false || $pageD!==false){
if($pageF!==false){
$number = $this->pageNumber%2==0?$this->pageNumber/2:($this->pageNumber+1)/2;
$DStart = $this->page - $number<0?$this->page - $number-1:0;
if($this->page+$number-$DStart > $this->pageToatl){
$DStop = ($this->page+$number-$DStart) - $this->pageToatl;
$forStop = $this->pageToatl+1;
}else{
$DStop = 0;
$forStop = $this->page+$number-$DStart+1;
}
$forStart = $this->page-$number-$DStop<1?1:$this->page-$number-$DStop;
for($i=$forStart;$i<$forStop;++$i){ if($i==$this->page){
$numberF .= ''.$i.' ';
}else{
$numberF .= ''.$i.' ';
}
}
}
if($pageD!==false){
$number = $this->pageNumber;
$forStart = $this->page-$number>0?$this->page-$number:1;
$forStop = $this->page+$number>$this->pageToatl?$this->pageToatl+1:$this->page+$number+1;
for($i=$forStart;$i<$this->page;++$i){
$numberD .= ''.$i.' ';
}
$numberD .= ''.$this->page.' ';
$start = $this->page+1;
for($i=$start;$i<$forStop;++$i){
$numberD .= ''.$i.' ';
}
}
$F = $forStart>1?'1 ':'';
$E = $forStop<$this->pageToatl+1?''.$this->pageToatl.' ':'';
if($forStart>2){
$omitF = '… ';
$startA = $this->page-$number<1?1:$this->page-$number;
$omitFA = '… ';
}
if($forStop<$this->pageToatl){
$omitE = '… ';
$stopA = $this->page+$number>$this->pageToatl?$this->pageToatl:$this->page+$number;
$omitEA = '… ';
}
}
$this->pageType = str_ireplace(array('%F%','%E%','%omitFA%','%omitEA%','%omitF%','%omitE%','%numberF%','%numberD%'),array($F,$E,$omitFA,$omitEA,$omitF,$omitE,$numberF,$numberD),$this->pageType);
}
/**
*Methods for processing url
* @access public
* @param array $url Keep the URL directly in the relationship array
* @return string filtered url tail parameters
*/
private function uri(){
$url = $_SERVER["REQUEST_URI"];
$par = parse_url($url);
if (isset($par['query'])) {
parse_str($par['query'],$query);
if(!is_array($this->uri)){
$this->uri = array();
}
array_merge($query,$this->uri);
unset($query[$this->pageParam]);
while($key = array_search('',$query)){
unset($query[$key]);
}
$this->uri = $par['path'].'?'.http_build_query($query);
}else{
$this->uri = $par['path'].'?';
}
}
/**
* Set the limit method and calculate the starting number and ending number
*/
private function calculate(){
$this->pageToatl = ceil($this->total/$this->pageSize);
$this->page = intval($_GET[$this->pageParam]);
$this->page = $this->page>=1?$this->page>$this->pageToatl?$this->pageToatl:$this->page:1;
$this->pageStart = ($this->page-1)*$this->pageSize;
$this->pageStop = $this->pageStart+$this->pageSize;
$this->pageStop = $this->pageStop>$this->total?$this->total:$this->pageStop;
$this->limit = $this->pageStart.','.$this->pageStop;
}
/**
* Set filters
*/
public function __set($name,$value){
switch($name){
case 'pageType':
case 'uri':
$this->$name = $value;
return;
case 'pageShow':
if(is_array($value)){
$this->pageShow = array_merge($this->pageShow,$value);
}
return;
}
}
/**
* Value filter
*/
public function __get($name){
switch($name){
case 'limit':
case 'pageStart':
case 'pageStop':
return $this->$name;
default:
return;
}
}
}
复制代码