PHP Pagination code example (Adaptive range)

Source: Internet
Author: User
  1. /*

  2. * MySQL Paging function code
  3. * edit:bbs.it-home.org
  4. *
  5. */
  6. Function page ($page, $total, $phpfile, $pagesize =10, $pagelen =7) {
  7. $pagecode = ";//define variables to store page-generated HTML
  8. $page = Intval ($page);//Avoid non-numeric page numbers
  9. $total = Intval ($total);//Guaranteed Total record value type is correct
  10. if (! $total) return array ();//The total number of records is zero returns an empty array
  11. $pages = Ceil ($total/$pagesize);//Calculation of total pages
  12. Handling page numbering legality
  13. if ($page <1) $page = 1;
  14. if ($page > $pages) $page = $pages;
  15. Calculate Query Offset
  16. $offset = $pagesize * ($page-1);
  17. Page range Calculation
  18. $init = 1;//Start Page number
  19. $max = $pages;//End page number
  20. $pagelen = ($pagelen%2)? $pagelen: $pagelen +1;//page number
  21. $pageoffset = ($pagelen-1)/2;//page number offset

  22. Generate HTML

  23. $pagecode = ";
  24. $pagecode. = "$page/$pages";//pages, total pages
  25. If this is the first page, the connection to the first and previous pages is not displayed
  26. if ($page!=1) {
  27. $pagecode. = "<<";//First page
  28. $pagecode. = "<";//prev
  29. }
  30. Can be offset when the number of pages is greater than the number
  31. if ($pages > $pagelen) {
  32. If the current page is less than or equal to the left offset
  33. if ($page <= $pageoffset) {
  34. $init = 1;
  35. $max = $pagelen;
  36. }else{//If the current page is greater than the left offset
  37. If the right offset of the current page number exceeds the Max page count
  38. if ($page + $pageoffset >= $pages + 1) {
  39. $init = $pages-$pagelen +1;
  40. }else{
  41. Calculations when left and right offsets are present
  42. $init = $page-$pageoffset;
  43. $max = $page + $pageoffset;
  44. }
  45. }
  46. }
  47. Generate HTML
  48. for ($i = $init; $i <= $max; $i + +) {
  49. if ($i = = $page) {
  50. $pagecode. = '. $i. ';
  51. } else {
  52. $pagecode. = "$i";
  53. }
  54. }
  55. if ($page! = $pages) {
  56. $pagecode. = ">";//Next page
  57. $pagecode. = ">>";//Last Page
  58. }
  59. $pagecode. = "";
  60. Return Array (' pagecode ' = $pagecode, ' sqllimit ' = ' limit '. $offset. ', ' $pagesize);
  61. }
  62. ?>

Copy Code

2, added the page jump text box

    1. $phpfile = ' index.php ';//page file name
    2. $page = isset ($_get[' page ')? $_get[' page ']:1;//default page number
    3. $db = mysql_connect (' localhost ', ' test ', ' test ');//LINK database
    4. mysql_select_db (' Test ', $db);//Select Database
    5. $counts = mysql_num_rows (mysql_query (' Select ' id ' from ' test ', $db));//Gets the total number of data required
    6. $sql = ' select ' id ', ' title ' From ' test ';//define Query statement SQL
    7. $getpageinfo = page ($page, $counts, $phpfile);//Call function, raw page HTML and SQL LIMIT clause
    8. $sql. = $getpageinfo [' sqllimit '];//combination of complete SQL statements
    9. $data = $row = Array ();//Initialize array
    10. $result = mysql_query ($sql, $db);//Get result set
    11. Loading data into the $data array
    12. while ($row = Mysql_fetch_array ($result)) {
    13. $data []= $row;
    14. }
    15. ?>
    16. echo $getpageinfo [' Pagecode '];//HTML code showing pagination
    17. ?>
Copy Code

3, the CSS style sheet file used in this paging query code.

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
  • Related Article

    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.