Code of two php paging programs-PHP source code

Source: Internet
Author: User
Create a PHP page splitter and automatically generate the page number. JS calls the function. The paging principle is roughly as follows. The paging program has two very important parameters: several records are displayed on each page ($ pagesize) and the current page ($ page ). Create a PHP page splitter and automatically generate the page number. JS calls the function. The paging principle is roughly as follows. The paging program has two very important parameters: several records are displayed on each page ($ pagesize) and the current page ($ page ).

Script ec (2); script

With these two parameters can be very convenient to write paging program, we take MySql database as the data source, in mysql if you want to retrieve a specific section of the table content can use the T-SQL statement: select * from table limit offset and rows. Here, offset is the record offset. Its calculation method is offset = $ pagesize * ($ page-1). rows is the number of records to be displayed. Here is $ page. That is to say, the select * from table limit 10, 10 statement means to retrieve 20 records starting from 11th records in the table.

The Code is as follows:

Class PageView {
/** Page number **/
Public $ pageNo = 1;
/** Page size **/
Public $ pageSize = 20;
/** Total number of pages **/
Public $ pageCount = 0;
/** Total number of records **/
Public $ totalNum = 0;
/** Offset, starting line of the current page **/
Public $ offSet = 0;
/** Data per page **/
Public $ pageData = array ();

/** Whether the previous page exists **/
Public $ hasPrePage = true;
/** Whether the next page exists **/
Public $ hasNextPage = true;

Public $ pageNoList = array ();

Public $ jsFunction = 'jsfunction ';
/**
*
* @ Param unknown_type $ count total number of rows
* @ Param unknown_type $ size page size
* @ Param unknown_type $ string
*/
Public function _ construct ($ count = 0, $ size = 20, $ pageNo = 1, $ pageData = array (), $ jsFunction = 'jsfunction '){

$ This-> totalNum = $ count; // total number of records
$ This-> pageSize = $ size; // size of each page
$ This-> pageNo = $ pageNo;
// Calculate the total number of pages
$ This-> pageCount = ceil ($ this-> totalNum/$ this-> pageSize );
$ This-> pageCount = ($ this-> pageCount <= 0 )? 1: $ this-> pageCount;
// Check pageNo
$ This-> pageNo = 0? 1: $ this-> pageNo;
$ This-> pageNo = $ this-> pageNo> $ this-> pageCount? $ This-> pageCount: $ this-> pageNo;

// Calculate the offset
$ This-> offset = ($ this-> pageNo-1) * $ this-> pageSize;
// Check whether the previous page is displayed
$ This-> hasPrePage = $ this-> pageNo = 1? False: true;

$ This-> hasNextPage = $ this-> pageNo >=$ this-> pageCount? False: true;

$ This-> pageData = $ pageData;
$ This-> jsFunction = $ jsFunction;

}
/**
* Paging algorithm
* @ Return
*/
Private function generatePageList (){
$ PageList = array ();
If ($ this-> pageCount <= 9 ){
For ($ I = 0; $ I <$ this-> pageCount; $ I ++ ){
Array_push ($ pageList, $ I + 1 );
}
} Else {
If ($ this-> pageNo <= 4 ){
For ($ I = 0; $ I <5; $ I ++ ){
Array_push ($ pageList, $ I + 1 );
}
Array_push ($ pageList,-1 );
Array_push ($ pageList, $ this-> pageCount );

} Else if ($ this-> pageNo> $ this-> pageCount-4 ){
Array_push ($ pageList, 1 );

Array_push ($ pageList,-1 );
For ($ I = 5; $ I> 0; $ I --){
Array_push ($ pageList, $ this-> pageCount-$ I + 1 );
}
} Else if ($ this-> pageNo> 4 & $ this-> pageNo <= $ this-> pageCount-4 ){
Array_push ($ pageList, 1 );
Array_push ($ pageList,-1 );

Array_push ($ pageList, $ this-> pageNo-2 );
Array_push ($ pageList, $ this-> pageNo-1 );
Array_push ($ pageList, $ this-> pageNo );
Array_push ($ pageList, $ this-> pageNo + 1 );
Array_push ($ pageList, $ this-> pageNo + 2 );

Array_push ($ pageList,-1 );
Array_push ($ pageList, $ this-> pageCount );

}
}
Return $ pageList;
}

/***
* Create a paging Control
* @ Param
* @ Return String
*/
Public function echoPageAsDiv (){
$ PageList = $ this-> generatePageList ();

$ PageString ="

";

If (! Empty ($ pageList )){
If ($ this-> pageCount> 1 ){
If ($ this-> hasPrePage ){
$ PageString = $ pageString. "jsFunction." (". ($ this-> pageNo-1).") "> previous page ";
}
Foreach ($ pageList as $ k => $ p ){
If ($ this-> pageNo = $ p ){
$ PageString = $ pageString. "". $ this-> pageNo ."";
Continue;
}
If ($ p =-1 ){
$ PageString = $ pageString ."...";
Continue;
}
$ PageString = $ pageString. "jsFunction." (". $ p.") ">". $ p ."";
}

If ($ this-> hasNextPage ){
$ PageString = $ pageString. "jsFunction." (". ($ this-> pageNo + 1).") "> next page ";
}

}
}
$ PageString = $ pageString .("

");
Return $ pageString;
}
}

?>

Css

The Code is as follows:

Usage

The Code is as follows:

$ PageNo = $ _ GET ['pageno'];
If (empty ($ pageNo )){
$ PageNo = 1;
}
// Paging data
$ PageData = News: getNewsPage ($ pageNo, $ pageSize );
// Obtain the total number of rows
$ Count = News: getNewsCount ();
// Create a page Splitter
$ P = new PageView ($ count ['0'] ['Total'], $ pageSize, $ pageNo, $ pageData );
// Generate page number
$ PageViewString = $ p-> echoPageAsDiv ();


Next we will introduce a paging class.

The Code is as follows:

Class SubPages {

Private $ each_disNums; // number of entries displayed on each page
Private $ nums; // The total number of items.
Private $ current_page; // the currently selected page
Private $ sub_pages; // The number of pages displayed each time
Private $ pageNums; // total number of pages
Private $ page_array = array (); // used to construct an array of pagination
Private $ subPage_link; // the link of each page
Private $ subPage_type; // type of the display page
/*
_ Construct is the SubPages constructor used to automatically run classes when they are created.
@ $ Each_disNums number of entries per page
@ Nums total number of entries
@ Current_num the selected page
@ Sub_pages the number of pages displayed each time
@ SubPage_link: the link of each page
@ SubPage_type: Type of the display page

Normal paging mode when @ subPage_type = 1
Example: A total of 4523 records are displayed. 10 records are displayed on each page. Currently, page 1/453 [homepage] [Previous Page] [Next Page] [Last Page]
Classic paging style when @ subPage_type = 2
Example: Current page 1/453 [homepage] [Previous Page] 1 2 3 4 5 6 7 8 9 10 [Next Page] [Last Page]
*/
Function _ construct ($ each_disNums, $ nums, $ current_page, $ sub_pages, $ subPage_link, $ subPage_type ){
$ This-> each_disNums = intval ($ each_disNums );
$ This-> nums = intval ($ nums );
If (! $ Current_page ){
$ This-> current_page = 1;
} Else {
$ This-> current_page = intval ($ current_page );
}
$ This-> sub_pages = intval ($ sub_pages );
$ This-> pageNums = ceil ($ nums/$ each_disNums );
$ This-> subPage_link = $ subPage_link;
$ This-> show_SubPages ($ subPage_type );
// Echo $ this-> pageNums. "--". $ this-> sub_pages;
}


/*
_ Destruct: A destructor called when the class is not in use. This function is used to release resources.
*/
Function _ destruct (){
Unset ($ each_disNums );
Unset ($ nums );
Unset ($ current_page );
Unset ($ sub_pages );
Unset ($ pageNums );
Unset ($ page_array );
Unset ($ subPage_link );
Unset ($ subPage_type );
}

/*
The show_SubPages function is used in the constructor. And used to determine what the page looks like
*/
Function show_SubPages ($ subPage_type ){
If ($ subPage_type = 1 ){
$ This-> subPageCss1 ();
} Elseif ($ subPage_type = 2 ){
$ This-> subPageCss2 ();
}
}


/*
The function used to initialize the array that creates pagination.
*/
Function initArray (){
For ($ I = 0; $ I <$ this-> sub_pages; $ I ++ ){
$ This-> page_array [$ I] = $ I;
}
Return $ this-> page_array;
}


/*
Construct_num_Page this function is used to construct display entries
Even if: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
*/
Function construct_num_Page (){
If ($ this-> pageNums <$ this-> sub_pages ){
$ Current_array = array ();
For ($ I = 0; $ I <$ this-> pageNums; $ I ++ ){
$ Current_array [$ I] = $ I + 1;
}
} Else {
$ Current_array = $ this-> initArray ();
If ($ this-> current_page <= 3 ){
For ($ I = 0; $ I
$ Current_array [$ I] = $ I + 1;
}
} Elseif ($ this-> current_page <= $ this-> pageNums & $ this-> current_page> $ this-> pageNums-$ this-> sub_pages + 1 ){
For ($ I = 0; $ I
$ Current_array [$ I] = ($ this-> pageNums)-($ this-> sub_pages) + 1 + $ I;
}
} Else {
For ($ I = 0; $ I
$ Current_array [$ I] = $ this-> current_page-2 + $ I;
}
}
}

Return $ current_array;
}

/*
Construct normal mode paging
A total of 4523 records are displayed. 10 records are displayed on each page. Currently, page 1/453 [homepage] [Previous Page] [Next Page] [Last Page]
*/
Function subPageCss1 (){
$ SubPageCss1Str = "";
$ SubPageCss1Str. = "Total". $ this-> nums. "records ,";
$ SubPageCss1Str. = "displayed on each page". $ this-> each_disNums ,";
$ SubPageCss1Str. = "current section". $ this-> current_page. "/". $ this-> pageNums. "Page ";
If ($ this-> current_page> 1 ){
$ FirstPageUrl = $ this-> subPage_link. "1 ";
$ PrewPageUrl = $ this-> subPage_link. ($ this-> current_page-1 );
$ SubPageCss1Str. = "[homepage]";
$ SubPageCss1Str. = "[Previous Page]";
} Else {
$ SubPageCss1Str. = "[homepage]";
$ SubPageCss1Str. = "[Previous Page]";
}

If ($ this-> current_page <$ this-> pageNums ){
$ LastPageUrl = $ this-> subPage_link. $ this-> pageNums;
$ NextPageUrl = $ this-> subPage_link. ($ this-> current_page + 1 );
$ SubPageCss1Str. = "[Next Page]";
$ SubPageCss1Str. = "[last page]";
} Else {
$ SubPageCss1Str. = "[Next Page]";
$ SubPageCss1Str. = "[last page]";
}

Echo $ subPageCss1Str;

}


/*
Create pagination in Classic Mode
Current page 1/453 [homepage] [Previous Page] 1 2 3 4 5 6 7 8 9 10 [Next Page] [Last Page]
*/
Function subPageCss2 (){
$ SubPageCss2Str = "";
$ SubPageCss2Str. = "current section". $ this-> current_page. "/". $ this-> pageNums. "Page ";


If ($ this-> current_page> 1 ){
$ FirstPageUrl = $ this-> subPage_link. "1 ";
$ PrewPageUrl = $ this-> subPage_link. ($ this-> current_page-1 );
$ SubPageCss2Str. = "[homepage]";
$ SubPageCss2Str. = "[Previous Page]";
} Else {
$ SubPageCss2Str. = "[homepage]";
$ SubPageCss2Str. = "[Previous Page]";
}

$ A = $ this-> construct_num_Page ();
For ($ I = 0; $ I
$ S = $ a [$ I];
If ($ s ==$ this-> current_page ){
$ SubPageCss2Str. = "[". $ s. "]";
} Else {
$ Url = $ this-> subPage_link. $ s;
$ SubPageCss2Str. = "[". $ s. "]";
}
}

If ($ this-> current_page <$ this-> pageNums ){
$ LastPageUrl = $ this-> subPage_link. $ this-> pageNums;
$ NextPageUrl = $ this-> subPage_link. ($ this-> current_page + 1 );
$ SubPageCss2Str. = "[Next Page]";
$ SubPageCss2Str. = "[last page]";
} Else {
$ SubPageCss2Str. = "[Next Page]";
$ SubPageCss2Str. = "[last page]";
}
Echo $ subPageCss2Str;
}
}
?>

Call Method

The Code is as follows:

Require_once ("SubPages. php ");
// Number of entries displayed per page
$ Page_size = 20;
// Total number of entries
$ Nums = 1024;
// Number of pages displayed each time
$ Sub_pages = 10;
// Obtain the current page
$ PageCurrent = $ _ GET ["p"];
// If (! $ PageCurrent) $ pageCurrent = 1;

$ SubPages = new SubPages ($ page_size, $ nums, $ pageCurrent, $ sub_pages, "test. php? P = ", 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.