Learning php paging code instances

Source: Internet
Author: User
When learning PHP, you will surely encounter the problem of operating the MYSQL database and displaying the data in the database by page. Below is a small example to learn the PHP paging method. PHP paging has many methods. today we will use a small example to demonstrate this function.
The code is as follows:
$ Result ="

    ";

    // Previous Page
    If ($ offset> 0 ){
    $ Result. ="

  • Prev
  • ";
    }

    $ Pages = $ allPageNums; // total number of pages
    $ Page = $ curPage; // Current page number
    $ Page_len = 9;
    $ Page_len = ($ page_len % 2 )? $ Page_len: $ pagelen + 1; // Number of page numbers
    $ Pageoffset = ($ page_len-1)/2; // page number offset between left and right
    If ($ pages> $ page_len ){
    // If the current page is less than or equal to the left offset
    If ($ page <= $ pageoffset ){
    $ Init = 1;
    $ Max_p = $ page_len;
    } Else {// if the current page is greater than the left offset
    // If the right offset of the current page number exceeds the maximum page number
    If ($ page + $ pageoffset >=$ pages + 1 ){
    $ Init = $ pages-$ page_len + 1;
    $ Max_p = $ pages;
    } Else {
    // Calculation when both the left and right offsets exist
    $ Init = $ page-$ pageoffset;
    $ Max_p = $ page + $ pageoffset;
    }
    }
    } Else {
      
    $ Init = 1;
    $ Max_p = $ pages;
    }

    For ($ I = $ init; $ I <= $ max_p; $ I ++ ){

    If ($ I ==$ curPage ){
    $ Result. ="

  • $ I
  • ";
    Continue;
    }

    $ Result. ="
  • $ I
  • ";

    }

    // Print the next page
    If ($ allnums> ($ offset + $ maxrow )){
    $ Result. ="

  • Next
  • ";
    }

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.