Php code for pagination of long articles
-
- /**
- * Author: wuniao heart
- * Code for pagination of long articles
- * Principle:
- * Use an array to record every page of the article (p0, p1, p2... manually mark), and then use the php function to operate the array to display the paging articles. Display by Page and pass the ptag value (same as the tag value.
- * Used php functions:
- * 1. strlen ("string")-Returns the length of the given string.-Returns the total number of bytes of the string.
- * 2. strpos ("string", "matching character")-Returns the numeric position of the first occurrence of needle in the haystack string. -returns the byte sequence of the first matched character in the string.
- * 3. substr ("string", "start position", "end position")-substr () returns the portion of string specified by the start and length parameters. -returns several characters in the string at the specified start and end positions.
- */
- $ SQL = "select * from article where id = 41"; // define an SQL statement and return content with id 41.
- $ Result = mysql_query ($ SQL); // executes the SQL statement and returns the result set.
- $ Row = mysql_fetch_array ($ result); // returns the result from the record set as an array.
- $ Content = $ row ['content']; // assigns an article to the variable $ content
- $ ArticleCounts = strlen ($ content); // returns the total number of bytes of $ content (article ).
- $ IsTrue = true; // Loop mark
- $ Tag = 0; // pagination tag and array subscript
- Echo "total bytes:". $ articleCounts ."
"; // Test information
- // Search for the Mark "ptag" and assign its location (number of bytes) to the array []
- While ($ isTrue ){
- $ StartAt = strpos ($ content, "p". $ tag); // Get the byte ordinal number of the corresponding ptag
- If ($ startAt! = False) {// if there is a flag (the returned value is not false), the record location
- $ Array [$ tag ++] = $ startAt;
- } Else {// if no tag exists, the array [0] is assigned a value of '\ 0'
- $ Array [$ tag] = '\ 0 ';
- $ IsTrue = false;
- }
- }
- // Mark position of cyclic output ----- test information
- For ($ I = 0; $ I <$ tag; $ I ++ ){
- Echo $ array [$ I]."
";
- }
- Echo "------------------------------
";
- // Output Content -----------
- If ($ array [0] = '\ 0') {// you can specify whether a tag exists.
- Echo $ content; // if no tag is displayed, it is displayed on a single page.
- } Else {// The tag is displayed on pages.
- // Output paging content
- If (isset ($ _ GET ['ptag']) {// determines whether ptag value is transmitted. if yes, page ptag + 1 is displayed, otherwise, the first page is displayed (ptag = 0)
- $ Ptag = $ _ GET ['ptag']; // assign the ptag value to the variable $ ptag
- If ($ ptag <$ tag) {// determines whether the parameter is correct
- Echo "pass by value, display page". ($ ptag + 1 )."
"; // Test information
- Echo "value:". $ ptag ."
"; // Test information
- Echo substr ($ content, $ array [$ ptag-1] + 2, $ array [$ ptag]-$ array [$ ptag-1]-2 ); // Display ptag + 1 page content
- } Else {echo "incorrect parameter ";}
- }
- Else {// if no ptag value is transferred, the first page (ptag = 0) is displayed)
- Echo "no value transfer, display page 1st
"; // Test information
- Echo substr ($ content, 0, $ array [0]-1); // display the content on the first page
- }
- }
- // Display the page number link cyclically ---
- If ($ array [0]! = '\ 0') {// displays the page number link only when manually marked
- For ($ I = 0; $ I <$ tag; $ I ++ ){
- If ($ ptag = $ I) {// if this page is displayed, it is in bold.
- $ Pager. ="". ($ I + 1 )."";
- } Else {// This page is not
- $ Pager. = "". ($ I + 1 )."";
- }
- }
- Echo"
Jump to the ". $ pager." page "; // Output link
- }
- ?>
|