Home >Backend Development >PHP Tutorial >PHP+mysql database query paging code example

PHP+mysql database query paging code example

WBOY
WBOYOriginal
2016-07-25 08:52:141173browse
  1. /*

  2. * php+mysql paging code
  3. *
  4. */

  5. $SQL_TABL="abc"; //table name

  6. $where_name="id > 10";//Query conditions
  7. $perpagenum = 3; //Number displayed per page

  8. $total = mysql_fetch_array(mysql_query("select count(*) AS count from $SQL_TABL WHERE (".@$where_name.")"));

  9. $sql_count=$total['count']; //Get the number of returned data
  10. unset($total); //Unregister the variable $total

  11. //Calculate the inaccurate number of pages $page_all_num_f is an integer, $page_all_num_t is an accurate value (may be a decimal)

  12. $page_all_num_f=round($page_all_num_t=$sql_count/$perpagenum, 0);
  13. if($page_all_num_f<$page_all_num_t) //Calculate the correct number of pages
  14. $page_all_num=$page_all_num_f+1;
  15. else
  16. $page_all_num=$page_all_num_f;

  17. if( is_numeric(@$_GET['p']) && @$_GET['p']>1 && @$_GET['p']<=$page_all_num) //Get the GET parameters to determine the current page

  18. $page_num= $_GET['p'];
  19. else
  20. $page_num=1;

  21. $sql_s_num=($page_num-1)*$perpagenum; //Calculate the starting number of data items

  22. $sql_p ="LIMIT ".$sql_s_num." , ".$perpagenum; //Generate database query code
  23. $result = mysql_query("select * from $SQL_TABL WHERE (".@$where_name.") ".$sql_p); / /Query data

  24. while($row=mysql_fetch_array($result)){ //Main loop

  25. //Output content
  26. }

  27. //Page button

  28. for($i=1;$i<=$page_all_num;$i++)
  29. {

  30. if(@$_GET[name]!=null) //Other GET parameters of the page

  31. $p_n="name=".@$_GET[name]."&";
  32. else
  33. $p_n=null;

  34. if($page_num==$i) //Current page Number emphasis

  35. $p_flag="class="flag" ";
  36. else
  37. $p_flag=null;

  38. echo "$in";

  39. }
  40. ?>

Copy code

Recommended reading:

  • php and ajax no refresh paging code
  • php article paging implementation code
  • php limit page turning (pagination) code
  • PHP paging class with multiple paging methods
  • PHP pagination code for previous page and next page
  • PHP paging code for the first ten pages and the next ten pages
  • Example of simple php pagination code
  • A good php pagination code
  • A paging function: Previous page Next page
  • A useful php paging class
  • php long article pagination code
  • A practical php paging class
  • Fast php paging class


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