A php paging class code similar to Aspnetpager is provided with source code download _ PHP Tutorial

Source: Internet
Author: User
A php paging code similar to Aspnetpager is provided with the source code for download. The basic logic is the same as that of. net. it is to replace the object class with an array for configuration. The logic is simple, and the paging html is spliced according to the condition judgment. The following basic logic is the same as that of. net. it is to replace the object class configuration with the array configuration. The logic is relatively simple, and the paging html is spliced according to the condition judgment.

It has the following simple functions:

1: displays or configures related buttons.
2: Supports Free configuration of each page, text name, and html tag class name
3: pages that support url rewriting (you need to add rewrite rules in the configuration array)

Simply put, go directly to the code:

Core code: pager. class. php

The code is as follows:


Class pager {
// Configure paging parameters
Private $ config = array (
// Text of the homepage button
"First_btn_text" => "homepage ",
// Text of the previous page button,
"Pre_btn_text" => "previous page ",
// Text on the next page
"Next_btn_text" => "next page ",
// Text on the last page,
"Last_btn_text" => "last page ",
// The total number of records * required
"Record_count" => 0,
// Page size per page
"Pager_size" => 10,
// The current page number * required
"Pager_index" => 1,
// Maximum number of buttons displayed on each page
"Max_show_page_size" => 10,
// The default value of the page number in the browser is page.
"Querystring_name" => "page ",
// Whether to rewrite the URL. the default value is flase.
"Enable_urlrewriting" => false,
// Url rewriting rules, for example, page/{page}. {page} indicates the page number.
"Urlrewrite_pattern" => "",
// The css name of the paging container
"Classname" => "paginator ",
// The class name of the button on the current page
"Current_btn_class" => "cpb ",
// Paginated text describes the css of the span tag
"Span_text_class" => "stc ",
/* Detailed jump text
* Totle indicates the total number of pages,
* Size indicates the number of entries per page.
* Goto indicates the input box to jump
* Record indicates the total number of records.
* Index indicates the current page number.
*/
"Jump_info_text" => "{totle} pages in total, {size} records per page, jump to the {goto} page ",
// Jump button text
"Jump_btn_text" => "OK ",
// Whether to display the jump
"Show_jump" => false,
// Whether to display the front button homepage & Previous Page
"Show_front_btn" => true,
// Whether to display the next and last buttons
"Show_last_btn" => true
);
/*
* Class constructor
* $ 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 ()
{
// Paging string
$ Pager_arr = array ();
// Size of each page
$ Pager_size = $ this-> config ["pager_size"];
// Obtain the 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;
// If the current page number is 0, it 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 set to 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 );
// Obtain the url to jump
$ Url = $ this-> get_url ();
// Add the beginning of p
$ Classname = $ this-> config ["classname"];
$ Pager_arr [] ="

\ N ";
// Add the html of the first two buttons
If ($ this-> config ["show_front_btn"])
{
// If the current page number is 1, the front 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 );
}
// Start 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;
// End of the current 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 ),"...");
}
// Page number of the subject
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 the html of the following two buttons
If ($ this-> config ["show_last_btn"])
{
// If the current page number is the last page, the last 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 html to jump
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 form a jump
$ Pager_arr [] = preg_replace ($ patterns, $ replacements, $ this-> config ["jump_info_text"]);
$ Btn_text = $ this-> config ['Dump _ btn_text '];
$ Pager_arr [] = "". $ this-> config ['Dump _ btn_text ']. "\ n ";
$ Pager_arr [] = $ this-> get_jumpscript ($ url );
}
$ Pager_arr [] ="

";
$ This-> config ["pager_index"] = $ pager_index;
Return implode ($ pager_arr );
}
/*
* Get the url to be processed. the configuration can be rewritten and the URLs of various parameters are supported.
*/
Private function get_url ()
{
// If url rewriting is allowed
If ($ this-> config ["enable_urlrewriting"])
{
// Obtain the url of the calling file
$ File_path = "http: //". $ _ SERVER ["HTTP_HOST"]. $ _ SERVER ["PHP_SELF"];
// Obtain the network directory of the called url
$ File_path = substr ($ file_path, 0, strripos ($ file_path ,"/"))."/";
// Directly add directory rewriting rules to form a new url
$ Url = $ file_path. $ this-> config ["urlrewrite_pattern"];
} Else {
// Obtain the absolute url of the current call page
$ Url = "http: //". $ _ SERVER ["HTTP_HOST"]. $ _ SERVER ["REQUEST_URI"];
// The name passed by the paging parameter in the browser
$ Querystring_name = $ this-> config ['querystring _ name'];
// If the url contains php? To replace the paging parameter.
If (strpos ($ url, "php? "))
{
// If page = xxx exists
$ Pattern = "/$ querystring_name = [0-9] */";
If (preg_match ($ pattern, $ url ))
{
// Replace the number in the words "page = ***" with {0}
$ Url = preg_replace ($ pattern, "$ querystring_name = {page}", $ url );
} Else {
$ Url. = "& $ querystring_name = {page }";
}
} Else {
// Directly add parameters to form the complete url of the page
$ Url. = "? $ Querystring_name = {page }";
}
}
Return $ url;
}
/*
* Obtain the html of Tag.
* $ Url: html to be directed to by tag
* $ Title: the title of a tag
** $ Attr: The additional attributes on the tag can be left empty.
*/
Private static function get_a_html ($ url, $ title, $ attr = "")
{
Return "$ title \ n ";
}
/*
* Obtain the html of the span tag
* $ Num: the text in the span, that is, the page number.
* $ Classname: class name of the span tag
*/
Private static function get_span_html ($ num, $ classname)
{
Return "$ num \ n ";
}
/*
* Format the url
* $ Original url
* $ Page number
*/
Private static function format_url ($ url, $ page)
{
Return preg_replace ("/\ {page \} $/", $ page, $ url );
}
/*
* Initialize the paging configuration file
* If this key value is not included in the parameter, the declarative value is used by default.
*/
Private function init_config ($ config)
{
// Determine whether the value exists, whether it is an array, and whether it contains records
If (isset ($ config) & is_array ($ config) & count ($ config)> 0 ){
Foreach ($ config as $ key => $ val)
{
$ This-> config [$ key] = $ val;
}
}
}
/*
* Method for constructing the redirection script
* $ Url: The url of the amount to be redirected
*/
Private function get_jumpscript ($ url)
{
$ Scriptstr ="

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.