Php code for pagination of long articles-php Tutorial

Source: Internet
Author: User
Php code for pagination of long articles

  1. /**
  2. * Author: wuniao heart
  3. * Code for pagination of long articles
  4. * Principle:
  5. * 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.
  6. * Used php functions:
  7. * 1. strlen ("string")-Returns the length of the given string.-Returns the total number of bytes of the string.
  8. * 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.
  9. * 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.
  10. */
  11. $ SQL = "select * from article where id = 41"; // define an SQL statement and return content with id 41.
  12. $ Result = mysql_query ($ SQL); // executes the SQL statement and returns the result set.
  13. $ Row = mysql_fetch_array ($ result); // returns the result from the record set as an array.
  14. $ Content = $ row ['content']; // assigns an article to the variable $ content
  15. $ ArticleCounts = strlen ($ content); // returns the total number of bytes of $ content (article ).
  16. $ IsTrue = true; // Loop mark
  17. $ Tag = 0; // pagination tag and array subscript
  18. Echo "total bytes:". $ articleCounts ."
    "; // Test information
  19. // Search for the Mark "ptag" and assign its location (number of bytes) to the array []
  20. While ($ isTrue ){
  21. $ StartAt = strpos ($ content, "p". $ tag); // Get the byte ordinal number of the corresponding ptag
  22. If ($ startAt! = False) {// if there is a flag (the returned value is not false), the record location
  23. $ Array [$ tag ++] = $ startAt;
  24. } Else {// if no tag exists, the array [0] is assigned a value of '\ 0'
  25. $ Array [$ tag] = '\ 0 ';
  26. $ IsTrue = false;
  27. }
  28. }
  29. // Mark position of cyclic output ----- test information
  30. For ($ I = 0; $ I <$ tag; $ I ++ ){
  31. Echo $ array [$ I]."
    ";
  32. }
  33. Echo "------------------------------
    ";
  34. // Output Content -----------
  35. If ($ array [0] = '\ 0') {// you can specify whether a tag exists.
  36. Echo $ content; // if no tag is displayed, it is displayed on a single page.
  37. } Else {// The tag is displayed on pages.
  38. // Output paging content
  39. 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)
  40. $ Ptag = $ _ GET ['ptag']; // assign the ptag value to the variable $ ptag
  41. If ($ ptag <$ tag) {// determines whether the parameter is correct
  42. Echo "pass by value, display page". ($ ptag + 1 )."
    "; // Test information
  43. Echo "value:". $ ptag ."
    "; // Test information
  44. Echo substr ($ content, $ array [$ ptag-1] + 2, $ array [$ ptag]-$ array [$ ptag-1]-2 ); // Display ptag + 1 page content
  45. } Else {echo "incorrect parameter ";}
  46. }
  47. Else {// if no ptag value is transferred, the first page (ptag = 0) is displayed)
  48. Echo "no value transfer, display page 1st
    "; // Test information
  49. Echo substr ($ content, 0, $ array [0]-1); // display the content on the first page
  50. }
  51. }
  52. // Display the page number link cyclically ---
  53. If ($ array [0]! = '\ 0') {// displays the page number link only when manually marked
  54. For ($ I = 0; $ I <$ tag; $ I ++ ){
  55. If ($ ptag = $ I) {// if this page is displayed, it is in bold.
  56. $ Pager. ="". ($ I + 1 )."";
  57. } Else {// This page is not
  58. $ Pager. = "". ($ I + 1 )."";
  59. }
  60. }
  61. Echo"
    Jump to the ". $ pager." page "; // Output link
  62. }
  63. ?>

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.