The basic logical idea is the same as. NET, which is to configure through the entity class to be configured through an array, logic is relatively simple, based on the criteria to determine the stitching of the paging HTML.
Here are a few simple features:
1: Support the display of related buttons or not configuration
2: Support per page number, text name, HTML tag class name free configuration
3: Support URL rewrite pages (you need to add rewrite rules in the configuration array yourself)
simple, or directly on the code:
Core code: pager.class.php
Copy CodeThe code is as follows:
Class pager{
Parameter configuration for paging
Private $config =array (
The text text of the home button
"First_btn_text" = "Home",
The text text of the previous page button,
"Pre_btn_text" = "prev",
Text on next page
"Next_btn_text" = "Next Page",
Text text on the last page,
"Last_btn_text" = "Last",
Total number of records * required
"Record_count" =>0,
Paging 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,
Page number the name passed in the browser defaults to page
"Querystring_name" = "page",
Whether the URL overrides default to Flase
"Enable_urlrewriting" =>false,
URL rewrite rules such as Page/{page} where {page} is the number of pages represented
"Urlrewrite_pattern" and "" ",
CSS name of the paging container
"ClassName" = "Paginator",
The class name of the current page button
"Current_btn_class" = "CPB",
CSS for paging text describing span tags
"Span_text_class" = "STC",
/* Detailed text for jump
*totle represents the total number of pages,
*size represents the number of pages per page
* Goto represents the input box to jump
* The record represents the total number of records
* Index represents the current page number
*/
"Jump_info_text" = "Total {totle} page, per page {size} record, jump to {goto} page",
The text of the Jump button
"Jump_btn_text" = "OK",
Whether to show jump
"Show_jump" =>false,
Do you want to show the front button home & Previous page
"Show_front_btn" =>true,
Do you want to show the Back button next & last Page
"Show_last_btn" =>true
);
/*
* Constructors for classes
* $config: Configuration of the paging class
*/
Public function __construct ($config)
{
$this->init_config ($config);
}
function __destruct ()
{
unset ($this->config);
}
/*
* Constructing paging main function
*/
Public Function Builder_pager ()
{
Paged string
$pager _arr=array ();
Size of each 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 placed as the last page
$pager _index= $pager _index>= $pager _num $pager _num: $pager _index;
Next Page page
$pager _next= $pager _index>= $pager _num $pager _num: ($pager _index+1);
Get the URL you want to jump to
$url = $this->get_url ();
Add a div at the beginning
$classname = $this->config["classname"];
$pager _arr[]= "\ n";
Add HTML for the previous two buttons
if ($this->config["SHOW_FRONT_BTN"])
{
If the current page number is 1 front the two buttons 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 starting 1~10 of the currently displayed page number 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 a jump to the previous layer of HTML
if ($pager _index> $pager _size)
{
$pager _arr[]= $this->get_a_html (Self::format_url ($url, $current _pager_start-1), "...");
}
Main Page 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 for the next two buttons
if ($this->config["SHOW_LAST_BTN"])
{
If the current page number is the last page, both 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 a jump to the HTML
if ($this->config["Show_jump"])
{
$patterns =array ("/\{totle\}/", "/\{size\}/", "/\{goto\}/", "/\{record\}/", "/\{index\}/",);
$replacements =array (
$pager _num,
$pager _size,
" \ n ",
$this->config["Record_count"],
$this->config["Pager_index"]
);
Replace a specific tag to make a jump
$pager _arr[]=preg_replace ($patterns, $replacements, $this->config["Jump_info_text");
$BTN _text= $this->config[' Jump_btn_text ';
$pager _arr[]= "". $this->config[' Jump_btn_text ']. "\ n ";
$pager _arr[]= $this->get_jumpscript ($url);
}
$pager _arr[]= "";
$this->config["Pager_index"]= $pager _index;
Return implode ($pager _arr);
}
/*
* Get URL to handle, support rewrite configuration, URL of various parameters
*/
Private Function Get_url ()
{
If the Allow URL override is set
if ($this->config["enable_urlrewriting"])
{
Get the URL where the file is called
$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, "/")). " /";
To create a new URL directly by attaching a table of contents rewrite rules
$url = $file _path. $this->config["Urlrewrite_pattern"];
}else{
Get the absolute URL of the current call page
$url = "http://". $_server["Http_host"].$_server["Request_uri"];
Name of the paging parameter passed in the browser
$querystring _name= $this->config[' querystring_name ';
What if PHP is included in the URL? String, you need to replace the paging parameter
if (Strpos ($url, "php?"))
{
If there is a page=xxx word
$pattern = "/$querystring _name=[0-9]*/";
if (Preg_match ($pattern, $url))
{
Replace the number in the typeface with page=*** with {0}
$url =preg_replace ($pattern, "$querystring _name={page}", $url);
}else{
$url. = "& $querystring _name={page}";
}
}else{
Direct additional parameters form the full URL of the paging
$url. = "? $querystring _name={page}";
}
}
return $url;
}
/*
* Get HTML for a tag
* $url: The HTML to be directed by a tag
* $title: Title of a label
* * $attr: Additional properties on a label may not be written
*/
private static function get_a_html ($url, $title, $attr = "")
{
return "$title \ n";
}
/*
* Get the HTML for the span tag
* $num: Text in span, which is the page number
* $classname: The class name of the span tag
*/
private static function get_span_html ($num, $classname)
{
Return " $num\ n ";
}
/*
* Format URL
* $url Original URL
* $page Page
*/
private static function Format_url ($url, $page)
{
Return Preg_replace ("/\{page\}$/", $page, $url);
}
/*
* Initialize the paging configuration file
* If the key value is not included in the parameter, the declared value is used by default
*/
Private Function Init_config ($config)
{
Determines whether the value exists, is an array, contains a record
if (Isset ($config) &&is_array ($config) &&count ($config) >0) {
foreach ($config as $key = $val)
{
$this->config[$key]= $val;
}
}
}
/*
* Method of constructing Jump function script
* $url: The amount of the URL that needs to be jumped
*/
Private Function Get_jumpscript ($url)
{
$scriptstr = "\ n";
return $scriptstr;
}
/*
* PHP constructs a function similar to the format method in. Net
* 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;
}
}
?>
called directly with the array parameter
Copy CodeThe code is as follows:
$config 1=array (
"Record_count" =>703,
"Pager_size" =>10,
"Show_jump" =>true,
"Pager_index" =>$_get["page"]
);
$pager _simple=new Pager ($config 1);
echo $pager _simple->builder_pager ();
?>
Finally, take a look at the demo's Picture:
Because the younger brother recently just learned PHP, the code appeared in the nonstandard, low efficiency, redundancy or design problems please give us a lot of advice.
Demo Source Download
http://www.bkjia.com/PHPjc/326058.html www.bkjia.com true http://www.bkjia.com/PHPjc/326058.html techarticle The basic logical idea is the same as. NET, which is to configure through the entity class to be configured through an array, logic is relatively simple, based on the criteria to determine the stitching of the paging HTML. Have the following several ...