<?php
class pager{
//Pagination parameter configuration
private $config=array(
//Text text of home page button
"first_btn_text"=>"Home",
//The text text of the previous page button,
"pre_btn_text"=>"Previous page",
//The text text on the next page
"next_btn_text"=>"Next Page",
//The text on the last page,
"last_btn_text"=>"last page",
//Total number of records *Required
"record_count"=>0,
//Page size per page
"pager_size"=>10,
//Current page number *Required
"pager_index"=>1,
//The maximum number of buttons displayed per page
"max_show_page_size"=>10,
//The name of the page number passing the value in the browser is page
"querystring_name"=>"page",
// Whether the URL is rewrited? Default is to flase
"enable_urlrewriting"=>false,
//url rewriting rules, for example, page/{page}, where {page} represents the number of pages
"urlrewrite_pattern"=>"",
//The css name of the pagination container
"classname"=>"paginator",
//The class name of the current page button
"current_btn_class"=>"cpb",
//Pagination text description of the span tag css
"span_text_class"=>"stc",
/* Detailed text for jump
*totle represents the total number of pages,
*size represents the number per page
* goto represents the input box to jump
* record represents the total number of records
* index represents the current page number
*/
"jump_info_text"=>"Total {totle} pages, {size} records per page, jump to {goto} page",
//Text of jump button
"jump_btn_text"=>"OK",
// Whether to display jump
"show_jump"=>false,
//Whether to display the previous buttons Home & Previous page
"show_front_btn"=>true,
//Whether to display the buttons next page & last page
"show_last_btn"=>true
);
/*
* class constructor
* $config:Configuration of this pagination class
*/
public function __construct($config)
{
$this->init_config($config);
}
function __destruct()
{
unset($this->config);
}
/*
* Construct the paged main function
*/
public function builder_pager()
{
//Pagination string
$pager_arr=array();
//Size per page
$pager_size=$this->config["pager_size"];
//Get a total number of pages
$pager_num=$this->config["record_count"]%$pager_size==0?$this->config["record_count"]/$pager_size:floor($this->config["record_count"]/$pager_size)+1;
//The current page number is set to 1 if it is 0
$pager_index=round($this->config["pager_index"])==0?1:round($this->config["pager_index"]);
//If the current page number is greater than or equal to the last page, the current page number is set to the last page
$pager_index=$pager_index>=$pager_num?$pager_num:$pager_index;
//The next page number
$pager_next=$pager_index>=$pager_num?$pager_num:($pager_index+1);
//Get the URL that needs to be redirected
$url=$this->get_url();
//Add the beginning div
$classname=$this->config["classname"];
$pager_arr[]="<div class=\"$classname\">\n";
//Add html for the first two buttons
if($this->config["show_front_btn"])
{
//If the current page number is 1, the front button will be disabled
$attr=$pager_index==1?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,1),$this->config["first_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_index-1),$this->config["pre_btn_text"],$attr);
}
//The beginning of the currently displayed page number 1~10 1 11~20 11
$current_pager_start=$pager_index%$pager_size==0?($pager_index/$pager_size-1)*$pager_size+1:floor($pager_index/$pager_size)*$pager_size+1;
//The end of the currently displayed page number
$current_pager_end=($current_pager_start+$pager_size-1)>=$pager_num?$pager_num:($current_pager_start+$pager_size-1);
//Add html to jump to the previous layer
if($pager_index>$pager_size)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_start-1),"...");
}
//Principal page number part
for($i=$current_pager_start;$i<=$current_pager_end;$i++)
{
if($i!=$pager_index)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$i),$i);
}else{
//If this is the current page
$pager_arr[]=$this->get_span_html($i,$this->config["current_btn_class"]);
}
}
//Add the next layer of html
if($pager_index<=($pager_num-$pager_num%$pager_size))
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_end+1),"...");
}
//Add html with the next two buttons
if($this->config["show_last_btn"])
{
//If the current page number is the last page, the last buttons will be disabled
$attr=$pager_index>=$pager_num?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_next),$this->config["next_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_num),$this->config["last_btn_text"],$attr);
}
//Add jump html
if($this->config["show_jump"])
{
$patterns=array("/\{totle\}/","/\{size\}/","/\{goto\}/","/\{record\}/","/\{index\}/",);
$replacements=array(
$pager_num,
$pager_size,
"<input type=\"input\" id=\"jumpNum\" style=\"width:20px;\" name=\"jump\" value=\"".$pager_next."\" />\n",
$this->config["record_count"],
$this->config["pager_index"]
);
//Replace specific tags to make jump
$pager_arr[]=preg_replace($patterns,$replacements,$this->config["jump_info_text"]);
$btn_text=$this->config['jump_btn_text'];
$pager_arr[]="<a href=\"javascript:void(0);\" style=\"float:none;\" onclick=\"javascript:jump();\">".$this->config['jump_btn_text']."</a></span>\n";
$pager_arr[]=$this->get_jumpscript($url);
}
$pager_arr[]="</div>";
$this->config["pager_index"]=$pager_index;
return implode($pager_arr);
}
/*
* Get the URL that needs to be processed, supports rewriting configuration, and urls with various parameters
*/
private function get_url()
{
//If you set the url rewrite
if($this->config["enable_urlrewriting"])
{
//Get the URL where the call file is located
$file_path="http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
//Get the network directory where the url is called
$file_path=substr($file_path,0,strripos($file_path,"/"))."/";
//Directly attach the directory to rewrite rules to form a new url
$url=$file_path.$this->config["urlrewrite_pattern"];
}else{
//Get the absolute url of the current calling page
$url="http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
//The name of the page parameter passed in the browser
$querystring_name=$this->config['querystring_name'];
//What if the url contains php? The string of the pager needs to be replaced
if(strpos($url,"php?"))
{
//If there is a word page=xxx
$pattern="/$querystring_name=[0-9]*/";
if(preg_match($pattern,$url))
{
//Replace the number in the word page=*** with {0}
$url=preg_replace($pattern,"$querystring_name={page}",$url);
}else{
$url.="&$querystring_name={page}";
}
}else{
// Directly attach parameters to form the complete url of the page
$url.="?$querystring_name={page}";
}
}
return $url;
}
/*
* Get the html with tag a
*$url: the html to which the tag a is to be directed
*$title:a tag title
**$attr:A additional attribute on the tag can be written without writing
*/
private static function get_a_html($url,$title,$attr="")
{
return "<a href='$url' $attr style=\"margin-right:5px;\">$title</a>\n";
}
/*
* Get the html of the span tag
* Text in $num:span, that is, page number
* $classname:span tag class name
*/
private static function get_span_html($num,$classname)
{
return "<span class=\"" .$classname. "\">$num</span>\n";
}
/*
* Format url
* $url Original url
* $page page number
*/
private static function format_url($url,$page)
{
return preg_replace("/\{page\}$/",$page,$url);
}
/*
*Initialize the page configuration file
*If the key value is not included in the parameter, the declared value is used by default.
*/
private function init_config($config)
{
//Judge whether the value exists, is an array, or contains records
if(isset($config)&&is_array($config)&&count($config)>0){
foreach($config as $key=>$val)
{
$this->config[$key]=$val;
}
}
}
/*
* Methods to construct jump function scripts
*$url: Which url needs to be redirected
*/
private function get_jumpscript($url)
{
$scriptstr = "<script type=\"text/javascript\">\n".
"function jump(){\n".
"var jnum=(\"jumpNum\").value;\n".
"if(isNaN(jnum)){\n".
"alert(\"Please enter the number in the jump box!\");\n".
"}\n".
"else{\n".
"var re=/\{page\}/\n".
"='$url'.replace(re,jnum);\n".
"}\n".
"}\n".
"</script>\n";
return $scriptstr;
}
/*
* Functions similar to format methods in .net are constructed in php
* Usage: format("hello,{0},{1},{2}", 'x0','x1','x2')
*/
private function format() {
$args = func_get_args();
if (count($args) == 0) { return;}
if (count($args) == 1) { return $args[0]; }
$str = array_shift($args);
$str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/', create_function('$match', '$args = '.var_export($args, true).'; return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];'), $str);
return $str;
}
}
?>