Home >Backend Development >PHP Tutorial >PHP code to implement paging display of long articles

PHP code to implement paging display of long articles

WBOY
WBOYOriginal
2016-07-25 09:03:40834browse
  1. /**
  2. *Author: Wuniao heart
  3. *Code to realize paging of long articles
  4. *Principle:
  5. *Use an array to record the starting number of bytes of each page of the article (manually marked with p0, p1, p2...) , and then use the php function to operate this array to display the paginated article. For paging display, pass the ptag (same as the value of tag) value.
  6. *PHP functions used:
  7. *1, strlen("string") - Returns the length of the given string. - Returns the total number of bytes in 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 of the first matching character that appears in the string Ordinal.
  9. *3, substr("string","start position","end position") - substr() returns the portion of string specified by the start and length parameters. - Returns a number of characters at the specified start and end positions in the string .
  10. */
  11. $sql = "select * from article where id = 41 ";//Define the sql statement and return the content with ID 41
  12. $result = mysql_query($sql);//Execute the sql statement and return the result set
  13. $row = mysql_fetch_array($result);//From the array in the form of an array The record set returns
  14. $content = $row['content'];//Assign the article to the variable $content
  15. $articleCounts = strlen($content);//Return the total number of bytes of $content (article)
  16. $isTrue = true;//Loop tag
  17. $tag = 0;//Paging tag, array subscript
  18. echo "Total number of bytes: ".$articleCounts."
    ";//Test information
  19. //Looking for tags" ptag", and assign its position (number of bytes) to the array array[]
  20. while($isTrue){
  21. $startAt = strpos($content,"p".$tag);//Get the corresponding ptag Byte number
  22. if($startAt != false){ //If there is a tag (the return value is not false), then record the position
  23. $array[$tag++] = $startAt;
  24. }else{ //If there is no tag, then Assign value to array array[0]'
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn