Php+mysql database query Pagination code example

Source: Internet
Author: User
  1. /*

  2. * Php+mysql Pagination Code
  3. *
  4. */

  5. $SQL _tabl= "ABC"; Table name

  6. $where _name= "ID > 10";//Query criteria
  7. $perpagenum = 3; Display quantity per page

  8. $total = Mysql_fetch_array (mysql_query ("SELECT count (*) as Count from $SQL _tabl WHERE (". @ $where _name. "));

  9. $sql _count= $total [' count ']; Gets the number of data bars returned
  10. Unset ($total); Unregister variable $total

  11. Calculates an inaccurate number of pages $page_all_num_f is an integer, $page _all_num_t is the exact value (possibly fractional)

  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 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; Number of data bars to start calculation

  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); Querying data

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

  25. Output content
  26. }

  27. Paging button

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

  30. if (@$_get[name]!=null)//page other GET parameters

  31. $p _n= "Name=". @$_get[name]. " & ";
  32. Else
  33. $p _n=null;

  34. if ($page _num== $i)//Emphasis of the current page number

  35. $p _flag= "class=\" flag\ "";
  36. Else
  37. $p _flag=null;

  38. echo "$i \ n";

  39. }
  40. ?>

Copy Code

Recommended reading:

    • PHP and Ajax No Refresh paging code
    • PHP article pagination Implementation code
    • PHP Limit Paging (paging) code
    • PHP Paging class with a variety of paging methods
    • Previous page and next page of PHP page code
    • PHP pagination code for the first 10 pages and the next 10 pages
    • Example of a simple PHP page-out code
    • A nice code for the PHP paging class
    • A paging function: previous page next page
    • A handy PHP paging class
    • PHP Long article pagination code
    • A useful PHP page-out class
    • Fast PHP page-out class
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.