Pagination of long articles using PHP

Source: Internet
Author: User
When using PHP to paging long articles, we usually display the content on pages for ease of reading and page display. Generally, pagination generates static files after multiple pagination when publishing an article in the background. This article uses examples to explain how to use PHP to dynamically process long articles by page.

View demo download source code

How to paging

Manual pagination: special pagination tags are usually added during content editing, such as {pages}. after submission, the PHP program processes pagination based on the pagination character to generate different static pages. This paging method provides accurate paging, but requires manual addition of paging characters, resulting in heavy workload.

Automatic paging: The PHP program will paginate the content based on the configured pagination character and then generate different static pages. This method is highly efficient and requires high requirements for processing tags of different html codes.

Front-end JS paging: uses Javascript to intercept long articles and display different sections based on requests to achieve paging effect. This method reads the content at a time, and the front-end js processes the page.

The example code in this article explains how to use PHP to pagination long article content, which can be automatically and manually paged. Generating static html pages is not covered in this article. we will introduce the generation of static pages later.

Paging type
 Page_word = $ page_word; $ this-> cut_tag = array ("","

","

","
","". ",". ",".","! ","...... ","? ",", "); $ This-> cut_custom =" {nextpage} "; $ tmp_page = intval (trim ($ _ GET [" ipage "]); $ this-> ipage = $ tmp_page> 1? $ Tmp_page: 1; $ this-> pagestr = $ pagestr;} function cut_str () {$ str_len_word = strlen ($ this-> pagestr ); // obtain the total number of characters obtained using strlen $ I = 0; if ($ str_len_word <= $ this-> page_word) {// if the total number of words is less than one page, display the number of words $ page_arr [$ I] = $ this-> pagestr;} else {if (strpos ($ this-> pagestr, $ this-> cut_custom) {$ page_arr = explode ($ this-> cut_custom, $ this-> pagestr);} else {$ str_first = substr ($ this-> pagestr, 0, $ this-> page_word); // 0-page _ The word cutStr is func. foreach ($ this-> cut_tag as $ v) {$ cut_start = strrpos ($ str_first, $ v ); // reverse query the position of the first paging character if ($ cut_start) {$ page_arr [$ I ++] = substr ($ this-> pagestr, 0, $ cut_start ). $ v; $ cut_start = $ cut_start + strlen ($ v); break; }}if ($ cut_start + $ this-> page_word) >=$ str_len_word) {// if the total number of words exceeds $ page_arr [$ I ++] = substr ($ this-> pagestr, $ cut_start, $ this-> page_word );} else {while ($ cut_star T + $ this-> page_word) <$ str_len_word) {foreach ($ this-> cut_tag as $ v) {$ str_tmp = substr ($ this-> pagestr, $ cut_start, $ this-> page_word); // The page_word character after the cut_start character $ cut_tmp = strrpos ($ str_tmp, $ v ); // find the position of the first paging character if ($ cut_tmp) between the page_word characters after the cut_start character) {$ page_arr [$ I ++] = substr ($ str_tmp, 0, $ cut_tmp ). $ v; $ cut_start = $ cut_start + $ cut_tmp + strlen ($ v); break ;}} if ($ cut_start + $ this-> pag E_word) >$ str_len_word) {$ page_arr [$ I ++] = substr ($ this-> pagestr, $ cut_start, $ this-> page_word );}}}} $ this-> sum_page = count ($ page_arr); // The total number of pages $ this-> pagearr = $ page_arr; return $ page_arr;} // display the previous function pagenav () {$ this-> set_url (); $ str = ''; // $ str. = $ this-> ipage. '/'. $ this-> sum_page; for ($ I = 1; $ I <= $ this-> sum_page; $ I ++) {if ($ I ==$ this-> ipage) {$ str. = "". $ I. "";} else {$ str. = "url. $ I. "'> ". $ I. "" ;}} Return $ str;} function set_url () {parse_str ($ _ SERVER ["QUERY_STRING"], $ arr_url ); unset ($ arr_url ["ipage"]); if (empty ($ arr_url) {$ str = "ipage =" ;}else {$ str = http_build_query ($ arr_url ). "& ipage =" ;}$ this-> url = "http ://". $ _ SERVER ["HTTP_HOST"]. $ _ SERVER ["PHP_SELF"]. "? ". $ Str ;}}?>

The above cutpage class can handle content paging very well and handle different html tags, causing paging troubles. If the content has a pagination character {nextpage}, the content is automatically paged by pagination character first.

Call paging class

We have read the text in text.txt. in actual projects, it should be the form that submits long content or reads the content of the database-related tables. The pagination class is instantiated, and the corresponding pagination content is called based on the current page, and the pagination entries are output.

 cut_str();  echo $page[$ipage-1];       echo $CP->pagenav();?>

It is worth noting that the use of a unified UTF-8 file encoding will make your coding work more smoothly.

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.