In-depth analysis of PHP article content paging _ PHP Tutorial

Source: Internet
Author: User
In-depth analysis of PHP article content page. There are two main methods for in-depth analysis of pagination of PHP article content: Method 1. paging by word count is easy to use, but the effect is not good. In-depth analysis of the content page of PHP articles

There are two main methods to pagination the article content:

Method 1. paging by word count

Paging by words is easy to use, but the effect is poor.

General Idea: first, set the maximum number of words per page. then, calculate the total number of words in the article content, and then calculate the total number of pages based on the total number of words and the maximum number of words on a single page. In this way, the preparation for the entire page is ready.

The content displayed on each page can be captured by content. For example, if the page contains 500 words and the article content contains 2200 words, the content between 501st and 1000 should be displayed when page = 2 is passed.

This method is simple, but may be difficult to display. the content of the article is usually accompanied by HTML tags. it is difficult to close HTML tags when cutting content. if this is not done well, the effect after paging is obviously not good.

Method 2: Paging by pagination

Paging with a paging character is more ideal than the first method.

General Idea: insert a pagination character (for example:

). When the document is displayed, the content of the article is divided. each part represents the content of a page, and the page number is displayed by passing parameter control.

This method is more user-friendly. after all, the paging content captured by manual control is more full of our thinking, and the HTML tag cannot be closed to some extent.

Page display
Paging display is one of the important means to batch send large volumes of data to the client. it is usually used to split the result set in the database into segments for display.

Category
PHP pages are divided into list pages and content pages. The basic principle is the same, whether it is list pages or content pages. data is sent to the client in batches.

Pager. class. php
This is a simple PHP paging display class. Currently, two paging modes are supported. One is the simplest normal paging mode [Homepage] [Previous Page] [Next Page] [last Page] mode, and the other is the classic paging mode, that is: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [Next Page] [last page].

Usage

The code is as follows:

Require_once 'pager. class. php ';
$ Pager = new pager ($ totalPage, $ currentPage); // $ pager object
Echo $ pager-> showpager (); // output page

Constructor of the display class on this page
/*
@ Total_page: total number of pages
@ Current_num current page
@ Sub_pages the number of pages displayed each time
@ SubPage_link: the link of each page
@ SubPage_type paging mode

Normal paging mode when @ subPage_type = 1
For example, a total of 4523 records are displayed. 10 records are displayed on each page. Currently, the [Homepage] [Previous Page] [Next Page] [last page]
Classic paging style when @ subPage_type = 2
For example: current page 1/453 [Homepage] [Previous Page] 1 2 3 4 5 6 7 8 9 10 [Next Page] [last page]
*/

The code is as follows:
Pager ($ total_page, $ current_page, $ sub_pages = 10, $ subPage_link = '', $ subPage_type = 2)

In the above two categories of PHP paging (list paging and content paging), I believe that list paging is no stranger to everyone. for content paging, the commonly used method is in the form of pagination (for example:

) Divide the content into multiple sections to find the total number of pages. use the current page number to obtain the page display list.

The code is as follows:

/**
* Example:
* * Require_once ("pager. class. php ");
* $ SubPages = new pager ($ totalPage, $ currentPage );
* Echo $ subPages-> showpager ();
*?>
**/
Class pager {

Var $ each_disNums; // Number of Entries displayed on each page
Var $ nums; // The total number of entries
Var $ current_page; // the currently selected page
Var $ sub_pages; // The number of pages displayed each time
Var $ pageNums; // The total number of pages
Var $ page_array = array (); // used to construct an array of pagination
Var $ subPage_link; // The link of each page
Var $ subPage_type; // display page type
Var $ _ lang = array (
'Index _ page' => 'homepage ',
'Pre _ page' => 'previous page ',
'Next _ page' => 'next page ',
'Last _ page' => 'Last page ',
'Current _ page' => 'Current page :',
'Total _ page' => 'total pages :',
'Current _ Show' => 'Current Display :',
'Total _ record '=>' total number of records :'
);
/*
_ Construct is the SubPages Constructor used to automatically run classes when they are created.
@ Total_page: total number of pages
@ 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 ($ total_page, $ current_page, $ sub_pages = 10, $ subPage_link = '', $ subPage_type = 2 ){
$ This-> pager ($ total_page, $ current_page, $ sub_pages, $ subPage_link, $ subPage_type );
}

Function pager ($ total_page, $ current_page, $ sub_pages = 10, $ subPage_link = '', $ subPage_type = 2 ){
If (! $ Current_page ){
$ This-> current_page = 1;
} Else {
$ This-> current_page = intval ($ current_page );
}
$ This-> sub_pages = intval ($ sub_pages );
$ This-> pageNums = ceil ($ total_page );
If ($ subPage_link ){
If (strpos ($ subPage_link ,'? Page = ') = false AND strpos ($ subPage_link,' & page = ') = false ){
$ SubPage_link. = (strpos ($ subPage_link ,'? ') = False? '? ':' & '). 'Page = ';
}
}
$ This-> subPage_link = $ subPage_link? $ SubPage_link: $ _ SERVER ['php _ SELF '].'? Page = ';
$ This-> subPage_type = $ subPage_type;
}

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


/*
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 pagelist1 (){
$ SubPageCss1Str = "";
$ SubPageCss1Str. = $ this-> _ lang ['current _ page']. $ this-> current_page. "/". $ this-> pageNums ."";
If ($ this-> current_page> 1 ){
$ FirstPageUrl = $ this-> subPage_link. "1 ";
$ PrewPageUrl = $ this-> subPage_link. ($ this-> current_page-1 );
$ SubPageCss1Str. = "{$ this-> _ lang ['index _ page']}";
$ SubPageCss1Str. = "{$ this-> _ lang ['pre _ page']}";
} Else {
$ SubPageCss1Str. = "{$ this-> _ lang ['index _ page']}";
$ SubPageCss1Str. = "{$ this-> _ lang ['pre _ page']}";
}

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

Return $ subPageCss1Str;
}


/*
Create an www.111cn.net page 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 pagelist2 (){
$ SubPageCss2Str = "";
$ SubPageCss2Str. = $ this-> _ lang ['current _ page']. $ this-> current_page. "/". $ this-> pageNums ."";

If ($ this-> current_page> 1 ){
$ FirstPageUrl = $ this-> subPage_link. "1 ";
$ PrewPageUrl = $ this-> subPage_link. ($ this-> current_page-1 );
$ SubPageCss2Str. = "{$ this-> _ lang ['index _ page']}";
$ SubPageCss2Str. = "{$ this-> _ lang ['pre _ page']}";
} Else {
$ SubPageCss2Str. = "{$ this-> _ lang ['index _ page']}";
$ SubPageCss2Str. = "{$ this-> _ lang ['pre _ 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. = "{$ this-> _ lang ['next _ page']}";
$ SubPageCss2Str. = "{$ this-> _ lang ['last _ page']}";
} Else {
$ SubPageCss2Str. = "{$ this-> _ lang ['next _ page']}";
$ SubPageCss2Str. = "{$ this-> _ lang ['last _ page']}";
}
Return $ subPageCss2Str;
}


/*
_ 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 );
}
}
?>

There are two main ways to paging the article content: Method 1. paging by words is easy to use, but the effect is not good. General Idea...

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.