In-depth analysis of PHP article content Paging
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,
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:
The Code is as follows: |
|
<? Php /** * Example: * <? Php * 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 <count ($ current_array); $ 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 <count ($ current_array); $ I ++ ){ $ Current_array [$ I] = ($ this-> pageNums)-($ this-> sub_pages) + 1 + $ I; } } Else { For ($ I = 0; $ I <count ($ current_array); $ 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. "& nbsp ;"; If ($ this-> current_page> 1 ){ $ FirstPageUrl = $ this-> subPage_link. "1 "; $ PrewPageUrl = $ this-> subPage_link. ($ this-> current_page-1 ); $ SubPageCss1Str. = "<a href = '$ firstPageUrl' >{$ this-> _ lang ['index _ page']} </a> "; $ SubPageCss1Str. = "<a href = '$ prewPageUrl' >{$ this-> _ lang ['pre _ page']} </a> "; } 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. = "<a href = '$ nextPageUrl' >{$ this-> _ lang ['Next _ page']} </a> "; $ SubPageCss1Str. = "<a href = '$ lastPageUrl' >{$ this-> _ lang ['last _ page']} </a> "; } 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. = "<a href = '$ firstPageUrl' >{$ this-> _ lang ['index _ page']} </a> "; $ SubPageCss2Str. = "<a href = '$ prewPageUrl' >{$ this-> _ lang ['pre _ page']} </a> "; } Else { $ SubPageCss2Str. = "{$ this-> _ lang ['index _ page']}"; $ SubPageCss2Str. = "{$ this-> _ lang ['pre _ page']}"; }
$ A = $ this-> construct_num_Page (); For ($ I = 0; $ I <count ($ a); $ I ++ ){ $ S = $ a [$ I]; If ($ s ==$ this-> current_page ){ $ SubPageCss2Str. = "[<span style = 'color: red; font-weight: bold; '>". $ s. "</span>]"; } Else { $ Url = $ this-> subPage_link. $ s; $ SubPageCss2Str. = "[<a href = '$ url'>". $ s. "</a>]"; } }
If ($ this-> current_page <$ this-> pageNums ){ $ LastPageUrl = $ this-> subPage_link. $ this-> pageNums; $ NextPageUrl = $ this-> subPage_link. ($ this-> current_page + 1 ); $ SubPageCss2Str. = "<a href = '$ nextPageUrl' >{$ this-> _ lang ['Next _ page']} </a> "; $ SubPageCss2Str. = "<a href = '$ lastPageUrl' >{$ this-> _ lang ['last _ page']} </a> "; } 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 ); } } ?> |