Home >Backend Development >PHP Tutorial >PHP paging principle example analysis, PHP simple paging code

PHP paging principle example analysis, PHP simple paging code

WBOY
WBOYOriginal
2016-07-25 08:52:43877browse
  1. $page=isset($_GET['page']) ? intval($_GET['page']) : 1; //Get the value of page in page=18, if If page does not exist, then the page number is 1.
  2. $num=10; //Display 10 pieces of data per page
  3. $db=mysql_connect("host","name","pass"); //Create a database connection
  4. $select=mysql_select_db("db",$db ); //Select the database to operate
  5. $total=mysql_num_rows(mysql_query("select * from table")); //The total number of query data
  6. $pagenum=ceil($total/$num); //Get the total pages Number
  7. //If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
  8. if($page>$pagenum || $page ==0){
  9. echo "Error : Can Not Found The page .";
  10. exit;
  11. }
  12. $offset=($page-1) * $num; //Get the value of the first parameter of limit, if the first page is (1-1)*10=0, the second page It is (2-1)*10=10.
  13. $info=mysql_query("select * from table limit$offset,$num"); //Get the data to be displayed for the corresponding page number
  14. while($it=mysql_fetch_array($info)){
  15. echo $it[' name']."
    ";
  16. for($i=1;$i<=$pagenum;$i++){
  17. $show=($i!=$page) ? "$i" : "$i";
  18. echo $show." ";
  19. }
  20. }
  21. ?>
Copy the code

You can replace the database connection and query tables with your own according to the above code, and test the paging effect.



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