Powerful php paging functions and php paging Functions

Source: Internet
Author: User

Powerful php paging functions and php paging Functions

Paging is what every program needs to understand. I have learned the same principle in several languages, next we will analyze php paging implementation for beginners and add a super powerful paging function at the end.

There are two main methods to pagination the article content:

Method 1,Paging by words

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. For example, a page can contain 500 words and 2200 words, when page = 2 is passed, the content between 501st and 1000 is displayed.

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.

Pagination: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 PagingList pageAndContent PagingThe basic principle is the same, whether it is list paging or content paging. 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].

The Code is as follows:

Require_once 'pager. class. php '; $ pager = new pager ($ totalPage, $ currentPage); // $ pager object echo $ pager-> showpager (); // output pagination the constructor of this pagination class/* @ total_page total page number @ current_num current page @ sub_pages the number of pages displayed each time @ subPage_link the link of each page @ subPage_type paging mode when @ subPage_type = 1 is the normal paging mode, for example: A total of 4523 records, 10 records per page, the current 1/453 page [homepage] [Previous Page] [Next Page] [last page] When @ subPage_type = 2 is a classic page style, for example: currently, 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:<Hr class = "pager">) Divide the content into multiple sections, find the total number of pages, and use the current page number to obtain the page display list. 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; // 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 (); // The array used to construct pagination var $ subPage_link; // the link of each pagination var $ subPage_type; // display the paging 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, it is used to automatically run when a class is created. @ total_page total number of pages @ current_num the currently selected page @ sub_pages the number of pages displayed each time @ subPage_link the link to each page @ subPage_type the type of the page displayed when @ subPage_type = 1 www.phpfensi.com is a normal page mode example: A total of 4523 records, 10 records per page, current page 1/453 [homepage] [Previous Page] [Next Page] [last page] 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 () ;}/ * is used to initialize the array for 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-> c Urrent_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 this-> current_page-2 + $ I; }}} Return $ current_array;}/* construct a total of 4523 records on Normal Mode pages. 10 records are displayed on each page, current 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. = "<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 A Classic Mode pagination 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 ['cur Pai_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']}"; $ s UbPageCss2Str. = "{$ 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 destructor, this function is called when the class is not 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) ;}}?>

Well, we only need to save it in a file and call it directly when it is used elsewhere. It is a very useful content or record paging functions, this is also an entry-level php tutorial.

Highlights: php Paging

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.