Php classic paging implementation code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags mysql tutorial
Php classic paging implementation code. This pagination class supports the form of displaying 1120 million records on a total of 20 pages on the previous page 400. for example, the page tag is used to control the url page. For example, xxx. php? PB_p in PB_page2 this paging class supports the form of 11/20 million records and 20 records per page on the previous page 11 12 13 14 1. for example, the page label is used to control the url page. For example, xxx. php? PB_page = PB_page in PB_page 2. it also supports the homepage at the bottom of the previous page and automatically obtains the url parameter page.

This page type can be displayed in the form of 11/20 million records on 400 pages, 20 records on the previous page, 11 12 13 14 1 on the previous page, such as the page tag, used to control the url page. For example, xxx. php Tutorial? Pb_page = pb_page in pb_page 2. it also supports the homepage at the bottom of the previous page and automatically obtains the url parameter page.


*/
Class minupage
{
/**
* Config, public
*/
Var $ page_name = "p"; // page tag, used to control url pages. For example, xxx. php? Pb_page = pb_page in 2
Var $ next_page = '>'; // Next page
Var $ pre_page = '<'; // Previous Page
Var $ first_page = 'first'; // home page
Var $ last_page = 'Lala'; // The last page.
Var $ pre_bar = '<'; // The Last Page
Var $ next_bar = '>'; // Next page
Var $ format_left = '';
Var $ format_right = '';
Var $ is_ajax = false; // whether the ajax paging mode is supported

/**
* Private
*
*/
Var $ pagebarnum = 10; // control the number of records.
Var $ totalpage = 0; // total number of pages
Var $ ajax_action_name = ''; // ajax action name
Var $ nowindex = 1; // current page
Var $ url = ""; // url header
Var $ offset = 0;

/**
* Constructor
*
* @ Param array $ array ['total'], $ array ['perpage'], $ array ['nowindex '], $ array ['URL'], $ array ['Ajax ']...
*/
Function minupage ($ array)
{
If (is_array ($ array )){
If (! Array_key_exists ('total', $ array) $ this-> error (_ function __, 'need a param of total ');
$ Total = intval ($ array ['total']);
$ Perpage = (array_key_exists ('perpage', $ array ))? Intval ($ array ['perpage']): 10;
$ Nowindex = (array_key_exists ('nowindex ', $ array ))? Intval ($ array ['nowindex ']): '';
$ Url = (array_key_exists ('URL', $ array ))? $ Array ['URL']: '';
} Else {
$ Total = $ array;
$ Perpage = 10;
$ Nowindex = '';
$ Url = '';
}
If ((! Is_int ($ total) | ($ total <0) $ this-> error (_ function __, $ total. 'is not a positive integer! ');
If ((! Is_int ($ perpage) | ($ perpage <= 0) $ this-> error (_ function __, $ perpage. 'is not a positive integer! ');
If (! Empty ($ array ['page _ name']) $ this-> set ('page _ name', $ array ['page _ name']); // set pagename
$ This-> _ set_nowindex ($ nowindex); // you can specify the current page.
$ This-> _ set_url ($ url); // sets the link address.
$ This-> totalpage = ceil ($ total/$ perpage );
$ This-> offset = ($ this-> nowindex-1) * $ perpage;
If (! Empty ($ array ['Ajax ']) $ this-> open_ajax ($ array ['Ajax']); // enable the ajax mode
}
/**
* Set the value of the variable name specified in the class. if the change volume does not belong to this class, throw an exception
*
* @ Param string $ var
* @ Param string $ value
*/
Function set ($ var, $ value)
{
If (in_array ($ var, get_object_vars ($ this )))
$ This-> $ var = $ value;
Else {
$ This-> error (_ function __, $ var. "does not belong to pb_page! ");
}

}
/**
* Open the inverted ajax mode.
*
* @ Param string $ action the default ajax-triggered action.
*/
Function open_ajax ($ action)
{
$ This-> is_ajax = true;
$ This-> ajax_action_name = $ action;
}
/**
* Get the code that displays the next page.
*
* @ Param string $ style
* @ Return string
*/
Function next_page ($ style = '')
{
If ($ this-> nowindex <$ this-> totalpage ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex + 1), $ this-> next_page, $ style );
}
Return ''. $ this-> next_page .'';
}

/**
* Get the code for displaying the "previous page"
*
* @ Param string $ style
* @ Return string
*/
Function pre_page ($ style = '')
{
If ($ this-> nowindex> 1 ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex-1), $ this-> pre_page, $ style );
}
Return ''. $ this-> pre_page .'';
}

/**
* Get the code for displaying the "homepage"
*
* @ Return string
*/
Function first_page ($ style = '')
{
If ($ this-> nowindex = 1 ){
Return ''. $ this-> first_page .'';
}
Return $ this-> _ get_link ($ this-> _ get_url (1), $ this-> first_page, $ style );
}

/**
* Get the code for displaying the "last page"
*
* @ Return string
*/
Function last_page ($ style = '')
{
If ($ this-> nowindex ==$ this-> totalpage ){
Return ''. $ this-> last_page .'';
}
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> totalpage), $ this-> last_page, $ style );
}

Function nowbar ($ style = '', $ nowindex_style = '')
{
$ Plus = ceil ($ this-> pagebarnum/2 );
If ($ this-> pagebarnum-$ plus + $ this-> nowindex> $ this-> totalpage) $ plus = ($ this-> pagebarnum-$ this-> totalpage + $ this-> nowindex );
$ Begin = $ this-> nowindex-$ plus + 1;
$ Begin = ($ begin> = 1 )? $ Begin: 1;
$ Return = '';
For ($ I = $ begin; $ I <$ begin + $ this-> pagebarnum; $ I ++)
{
If ($ I <= $ this-> totalpage ){
If ($ I! = $ This-> nowindex)
$ Return. = $ this-> _ get_text ($ this-> _ get_link ($ this-> _ get_url ($ I), $ I, $ style ));
Else
$ Return. = $ this-> _ get_text (''. $ I .'');
} Else {
Break;
}
$ Return. = "n ";
}
Unset ($ begin );
Return $ return;
}
/**
* Get the code for displaying the jump button
*
* @ Return string
*/
Function select ($ url)
{
$ Return ='';For ($ I = 1; $ I <= $ this-> totalpage; $ I ++){If ($ I ==$ this-> nowindex ){$ Return. =''. $ I .'';} Else {$ Return. =''. $ I .'';}}Unset ($ I );$ Return. ='';
Return $ return;
}

/**
* Obtain the limit value in the mysql tutorial statement.
*
* @ Return string
*/
Function offset ()
{
Return $ this-> offset;
}

/**
* Control the display style by page (you can add the corresponding style)
*
* @ Param int $ mode
* @ Return string
*/
Function show ($ mode = 1, $ url = '')
{
Switch ($ mode)
{
Case '1 ':
$ This-> next_page = 'next page ';
$ This-> pre_page = 'previous page ';
Return $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page (). 'Di '. $ this-> select ($ url ). 'Page ';
Break;
Case '2 ':
$ This-> next_page = 'next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'Last page ';
Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'Di '. $ this-> select ($ url ). 'Page ';
Break;
Case '3 ':
$ This-> next_page = 'next page ';
$ This-> pre_page = 'previous page ';
$ This-> first_page = 'homepage ';
$ This-> last_page = 'Last page ';
Return $ this-> first_page (). $ this-> pre_page (). $ this-> next_page (). $ this-> last_page ();
Break;
Case '4 ':
$ This-> next_page = 'next page ';
$ This-> pre_page = 'previous page ';
Return $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page ();
Break;
Case '5 ':
Return $ this-> pre_bar (). $ this-> pre_page (). $ this-> nowbar (). $ this-> next_page (). $ this-> next_bar ();
Break;
}

}
/* ---------------- Private function (private method )-----------------------------------------------------------*/
/**
* Set the url header address
* @ Param: string $ url
* @ Return boolean
*/
Function _ set_url ($ url = "")
{
If (! Empty ($ url )){
// Manually set
$ This-> url = $ url. (stristr ($ url ,'? '))? '&':'? '). $ This-> page_name. "= ";
} Else {
// Automatically obtain
If (empty ($ _ server ['query _ string']) {
// When query_string does not exist
$ This-> url = $ _ server ['request _ uri ']. "? ". $ This-> page_name." = ";
} Else {
//
If (stristr ($ _ server ['query _ string'], $ this-> page_name. '= ')){
// The address has page parameters.
$ This-> url = str_replace ($ this-> page_name. '='. $ this-> nowindex, '', $ _ server ['request _ uri ']);
$ Last = $ this-> url [strlen ($ this-> url)-1];
If ($ last = '? '| $ Last = '&'){
$ This-> url. = $ this-> page_name. "= ";
} Else {
$ This-> url. = '&'. $ this-> page_name. "= ";
}
} Else {
//
$ This-> url = $ _ server ['request _ uri '].' & '. $ this-> page_name.' = ';
} // End if
} // End if
} // End if
}

/**
* Set the current page
*
*/
Function _ set_nowindex ($ nowindex)
{
If (empty ($ nowindex )){
// System retrieval

If (isset ($ _ get [$ this-> page_name]) {
$ This-> nowindex = intval ($ _ get [$ this-> page_name]);
}
} Else {
// Manually set
$ This-> nowindex = intval ($ nowindex );
}
}

/**
* Return the address value for the specified page.
*
* @ Param int $ pageno
* @ Return string $ url
*/
Function _ get_url ($ pageno = 1)
{
Return $ this-> url. $ pageno;
}

/**
* Get the text displayed by page. for example, by default, _ get_text ('1') will return [1].
*
* @ Param string $ str
* @ Return string $ url
*/
Function _ get_text ($ str)
{
Return $ this-> format_left. $ str. $ this-> format_right;
}

/**
* Obtain the link address
*/
Function _ get_link ($ url, $ text, $ style = ''){
$ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
If ($ this-> is_ajax ){
// If the ajax mode is used
Return 'Ajax _ action_name. '(''. $ url.'') ">'. $ text .'';
} Else {
Return ''. $ text .'';
}
}
/**
* Error handling method
*/
Function error ($ function, $ errormsg)
{
Die ('error in file'. _ File __.', Function'. $ Function .'(): '. $ Errormsg );
}

/*
* Calculate the total number of pages on the front-end page.
* @ Param total number of records
* @ Param num the number of records per page
*/
Function sumpage ($ total, $ num)
{
$ Nn = 0;
$ Nn = intval ($ total/$ num );
If (int) ($ total % $ num )! = 0)
{
$ Nn = $ nn + 1;
}
Return $ nn;
}

}

/*
Foreground code call
Function minupage ($ array): constructor, parameter (array (total number of pages, number of rows per page, nowindex current page, url address, ajax mode ))

Use case:
$ Num = 20; // set the number of records displayed on each page
$ P = isset ($ _ get ['P'])? Intval ($ _ get ['P']): 1; // Obtain the passed value. if it is null, it is zero.
$ Total = newssum ($ sort); // The total number of records obtained
$ Page = new minupage (array ('total' => $ total, 'perpage' => $ num); // call the paging class
$ P_start = ($ P-1) * $ num; // calculates the number of records starting from the page.
$ Sumpage = & $ page-> sumpage ($ total, $ num); // total number of pages

/ Total pages Records, Entries/page, Show (4);?> Displayed as 11/20 page times 400 records 20 records/Page Previous 11 12 13 14 15 Next

Paging call method

Require_once ('../libs/www. bKjia. c0m/page. class. php ');
$ Page = new minupage (array ('total' => 1000, 'perpage' => 20 ));
Echo 'MoDe: 1
'. $ Page-> show ();
Echo 'MoDe: 2
'. $ Page-> show (2 );
Echo 'MoDe: 3
'. $ Page-> show (3 );
Echo 'MoDe: 4
'. $ Page-> show (4 );
Echo 'start ajax mode :';
$ Ajaxpage = new minupage (array ('total' => 1000, 'perpage' => 20, 'Ajax '=> 'Ajax _ page ', 'page _ name' => 'test '));
Echo 'MoDe: 1
'. $ Ajaxpage-> show ();
*/

11/20 page times 400 records 20 records/page Previous 11 12 13 14 1 form, such as the page tag, used to control the url page. For example, xxx. php? PB_p in PB_page = 2...

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.