Home  >  Article  >  Backend Development  >  PHP paging principle simple paging code summary

PHP paging principle simple paging code summary

WBOY
WBOYOriginal
2016-07-25 08:52:511000browse
  1. //Determine how many items are displayed on each page

  2. //count(*) Query the total number of items
  3. //Calculate the total number of pages ceil()
  4. //Judge $_GET and assign the current page number
  5. //mysql query limit (current page number-1)*number of items per page, number of items per page
  6. //If the total number of items is not empty, loop mysql_fetch_array()
  7. //$_SERVER['PHP_SELF']
  8. //Previous page?page=Current page-1
  9. //Next page?page=Current page+1
  10. //edit: bbs.it-home.org
  11. include("conn.php" );
  12. $perpagenum = 3;
  13. $total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM message"));
  14. $Total = $total[0];
  15. $Totalpage =ceil($Total/$perpagenum) ;

  16. if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage ){

  17. $page = 1;
  18. }else{
  19. $page = $_GET['page'];
  20. }

  21. $start = ($page-1)*$perpagenum;

  22. $sql = "SELECT * FROM message ORDER BY id DESC LIMIT $start,$perpagenum";
  23. $result = mysql_query($sql);
  24. $contents = mysql_fetch_array($result);
  25. ?>
  26. if($total){
  27. do{
  28. $user = $contents['user'];
  29. $contact = $contents['contact'];
  30. $content = $contents['content'];
  31. $date = $contents['date'];

  32. ?>

  33. < ;td width="193" height="22">Nickname:
  34. }
  35. //php paging starts
  36. while($contents=mysql_fetch_array($result));
  37. $per = $page-1;
  38. $next= $page+1;
  39. echo "
    Total".$ Total." comments, ".$perpagenum." messages per page, total ".$page."/".$Totalpage." pages. ";
  40. if($page!=1){
  41. echo "Homepage".
  42. "Previous page";
  43. }
  44. if($page!=$Totalpage){
  45. echo "Next page";
  46. echo "Last page";
  47. }
  48. ?>
  49. }
  50. ?>
  51. Posted in:

Copy code


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