Home > Backend Development > PHP Tutorial > PHP paging principle example tutorial, PHP paging principle demonstration

PHP paging principle example tutorial, PHP paging principle demonstration

WBOY
Release: 2016-07-25 08:52:46
Original
1147 people have browsed it
  1. //Connect to the database
  2. $con = mysql_connect("localhost","root","");
  3. mysql_select_db("xueshengchu",$con);
  4. mysql_query("set names utf8 ");
  5. $pageSize = 5; //Number of data items displayed on each page
  6. $result = mysql_query("select * from stu_msg");
  7. $totalNum = mysql_num_rows($result); //Total number of data items
  8. $totalPageCount = intval($totalNum/$pageSize); //Total number of pages
  9. //Determine which page the current page is
  10. $nowPage = isset($_GET['page']) ? intval($_GET[' page']) : 1;
  11. //Previous page
  12. $prev = ($nowPage-1 //Next page
  13. $next = ($nowPage+1 >= $totalPageCount) ? $totalPageCount : $nowPage+1;
  14. //Offset
  15. $offset = ($nowPage-1)*$pageSize;
  16. /*You can get data, SQL statement paging principle
  17. * select * from table limit $offset,$size paging execution statement
  18. */ bbs.it-home.org
  19. $sql = "select * from stu_msg limit $offset,$pageSize";
  20. $result = mysql_query($sql, $con);
  21. while($arr = mysql_fetch_array($result)){
  22. echo $arr['mid'].$arr['content']."
    ";
  23. }
  24. echo "< ;a href="".$_SERVER['PHP_SELF']."?page=1">Homepage";
  25. echo "Previous page";
  26. echo "Next page";
  27. echo "Last page< ;/a>";
  28. ?>
Copy code

php paging code demonstration effect: PHP paging principle example tutorial, PHP paging principle demonstration

Code description: The php code above the png has been commented very clearly

Analysis:

1. The global variable $_SERVER['PHP_SELF'] is the absolute path to obtain script variables.

2. The essence of paging is to display the database data piece by piece. For example, in the above PHP example: Take out the first 5 items (first page): select * from table limit 0,5 Take out items 5 to 1 and 10 (second page): select * from table limit 5,5 This gives us the offset formula: $offset = ($nowPage-1)*$pageSize;

3. The intval() function is to get the integer value of the variable



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template