Home > Backend Development > PHP Tutorial > PHP pagination function displays data

PHP pagination function displays data

WBOY
Release: 2016-07-25 08:50:50
Original
1130 people have browsed it
Recently, Mo Fangshu's website created a page with all comments and directly called some fields in the wp database. This is not difficult. For me, a newbie who just learned PHP, the most difficult thing is the paging function. At the beginning, I used the most Basic paging code is implemented, but the code and experience are very poor. It only realizes page turning up and down. Later I found this function, which is not bad. I would like to share it:
Create a mypage.php file and load it later.
  1. if(!function_exists("pageDivide")){
  2. #$total total information
  3. #$shownu display quantity, default 20
  4. #$url link to this page
  5. function pageDivide($total,$ shownu=20,$url=''){
  6. #$page current page number
  7. #$sqlfirst mysql database starting item
  8. #$pagecon paging navigation content
  9. global $page,$sqlfirst,$pagecon,$_SERVER;
  10. $GLOBALS ["shownu"]=$shownu;
  11. if(isset($_GET['page'])){
  12. $page=$_GET['page'];
  13. }else $page=1;
  14. #if$ The url uses the default value, which is a null value, and the value is assigned to the URL of this page
  15. if(!$url){ $url=$_SERVER["REQUEST_URI"];}
  16. #URL analysis
  17. $parse_url=parse_url($url);
  18. @$url_query=$parse_url["query"]; //Get the content after the question mark?
  19. if($url_query){
  20. $url_query=preg_replace("/(&?)(page=$page)/","" ,$url_query);
  21. $url = str_replace($parse_url["query"],$url_query,$url);
  22. if($url_query){
  23. $url .= "&page";
  24. }else $url .= " page";
  25. }else $url .= "?page";
  26. #Page number calculation
  27. $lastpg=ceil($total/$shownu); //Last page, total number of pages
  28. $page=min($lastpg, $page);
  29. $prepg=$page-1; //Previous page
  30. $nextpg=($page==$lastpg ? 0 : $page+1); //Next page
  31. $sqlfirst=($page -1)*$shownu;
  32. #Start paging navigation content
  33. $pagecon = "Show first".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total )." records, a total of $total records";
  34. if($lastpg<=1) return false; //If there is only one page, jump out
  35. if($page!=1 ) $pagecon .=" Homepage "; else $pagecon .="Homepage";
  36. if($prepg) $pagecon .=" Previous page "; else $pagecon .=" Previous page";
  37. if($nextpg) $pagecon .=" Next page "; else $pagecon .=" Next page";
  38. if($page!=$lastpg) $pagecon.=" Last page "; else $pagecon .=" Last page";
  39. # Drop down jump list, loop through all page numbers
  40. $pagecon .="Go to page, total $lastpg page";
  41. }
  42. }else die('pageDivide() function with the same name already exists!');
  43. ?>
Copy code
  1. require_once('mypage.php');
  2. $result=mysql_query("select * from table", $myconn);
  3. $total=mysql_num_rows($result); //Get the total number of information
  4. pageDivide($total,10); //Call the paging function, parameter two is the number of items displayed on each page
  5. //Database operation, select the data of your own database, this is just an example, the database link is closed, there is no operation here Explained.
  6. $result=mysql_query("select * from table limit $sqlfirst,$shownu", $myconn);
  7. while($row=mysql_fetch_array($result)){
  8. //Data operation
  9. }
  10. echo $pagecon; // Output paginated navigation content
  11. ?>
Copy code


Related labels:
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