Imitation Aspnetpager of a php page-like code source download _php Instance

Source: Internet
Author: User
Tags goto learn php prev
The basic logical thinking and. NET, is to be configured through the entity classes replaced by the array configuration, the logic is relatively simple, based on the conditions to determine the Mosaic page HTML.

Here are a few simple features:

1: Support the display of related buttons or not configuration
2: Support the number of pages per page, text name, HTML tag class name free configuration
3: URL-rewritten pages are supported (you need to add rewrite rules to the configuration array yourself)

simple, or directly on the code:

Core code: pager.class.php
Copy Code code as follows:

<?php
Class pager{
Paging parameter configuration
Private $config =array (
The text of the first button
"First_btn_text" => "Home",
The text text of the previous page button,
"Pre_btn_text" => "prev",
Text text for the next page
"Next_btn_text" => "Next Page",
The text text of the last page,
"Last_btn_text" => "Last",
Total Record number * required
"Record_count" =>0,
Page size per page
"Pager_size" =>10,
Current page number * required
"Pager_index" =>1,
Maximum number of buttons per page display
"Max_show_page_size" =>10,
The name of the page number in the browser that passes the value 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 representative number of pages
"Urlrewrite_pattern" => "",
CSS name of the paging container
"ClassName" => "Paginator",
Class name of the current page button
"Current_btn_class" => "CPB",
Pagination text describes a span tag's CSS
"Span_text_class" => "STC",
/* The detailed text of the 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",
Text of a Jump button
"Jump_btn_text" => "OK",
Whether to show jumps
"Show_jump" =>false,
Do you want to show the front button first & prev
"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);
}
/*
* Construct the paging main function
*/
Public Function Builder_pager ()
{
Page-Splitting string
$pager _arr=array ();
Size of each page
$pager _size= $this->config["Pager_size"];
Get a total number of pagination
$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, if it is 0, is set to 1
$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;
Page number of the next page
$pager _next= $pager _index>= $pager _num $pager _num: ($pager _index+1);
Get the URL that needs to jump
$url = $this->get_url ();
Add an opening div
$classname = $this->config["classname"];
$pager _arr[]= "<div class=\" $classname \ ">\n";
Add HTML for front two buttons
if ($this->config["SHOW_FRONT_BTN"])
{
If the current page number is 1, front these 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 current page number starts 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 an HTML that jumps to the previous level
if ($pager _index> $pager _size)
{
$pager _arr[]= $this->get_a_html (Self::format_url ($url, $current _pager_start-1), "...");
}
Main Page Section
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 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 back two buttons
if ($this->config["SHOW_LAST_BTN"])
{
If the current page number is the last page, then these two 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 jumps to 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 a specific label composition 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, support the rewrite configuration, the URL of various parameters
*/
Private Function Get_url ()
{
If the Allow URL rewrite is set
if ($this->config["enable_urlrewriting"])
{
Get the URL where the calling file is located
$file _path= "http://" $_server["Http_host"].$_server["php_self"];
Get the network directory where the calling URL resides
$file _path=substr ($file _path,0,strripos ($file _path, "/")). " /";
To create a new URL directly by attaching a directory-overriding rule
$url = $file _path. $this->config["Urlrewrite_pattern"];
}else{
Gets the absolute URL of the current calling page
$url = "http://" $_server["Http_host"].$_server["Request_uri"];
The name passed in the browser by the paging parameter
$querystring _name= $this->config[' querystring_name '];
If PHP is included in this 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 containing page=*** with {0}
$url =preg_replace ($pattern, "$querystring _name={page}", $url);
}else{
$url. = "& $querystring _name={page}";
}
}else{
Direct attach parameters to form a complete URL for pagination
$url. = "$querystring _name={page}";
}
}
return $url;
}
/*
* Get HTML for a label
* $url: The HTML to be directed by a label
* $title: Title of a label
* * $attr: An attached property on a label can be written without
*/
private static function get_a_html ($url, $title, $attr = "")
{
Return "<a href= ' $url ' $attr style=\" margin-right:5px;\ "> $title </a>\n";
}
/*
* Get the SPAN label html
* $num: Text in span, page ordinal
* $classname: The class name of the span label
*/
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 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 records
if (Isset ($config) &&is_array ($config) &&count ($config) >0) {
foreach ($config as $key => $val)
{
$this->config[$key]= $val;
}
}
}
/*
* Method of constructing the Jump function script
* $url: The amount of the URL that needs to be jumped
*/
Private Function Get_jumpscript ($url)
{
$scriptstr = "<script type=\" text/javascript\ ">\n".
"Function jump () {\ n".
"Var Jnum=document.getelementbyid (\" jumpnum\ "). value;\n".
' If (isNaN (jnum)) {\ n '.
"Alert (\) in the Jump box, enter a number!" \ "); \ n".
"}\n".
"Else{\n".
"Var re=/\{page\}/\n".
"Location.href= ' $url '. replace (re,jnum); \ n".
"}\n".
"}\n".
"</script>\n";
return $scriptstr;
}
/*
* Constructs a function similar to the format method in. NET 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;
}
}
?>

called directly with array arguments
Copy Code code as follows:

<?php
$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 look at the demo of the Picture:

Because the younger brother recently just learn PHP, the code appears in the nonstandard, inefficient, redundant or design problems also please advise.

Demo Source Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.