The five rings of the song of PHP pagination

Source: Internet
Author: User

Absrtact: According to the experience of the past, for many beginners, the paging function is still difficult to achieve, in order to help you solve the problem: the five ring song. From this paging function is like small Yeu Yue singing as concise, beautiful, capable. Haha, no chatter. Formally into the thousand-front php five-ring song of the steps to achieve paging function.

According to previous experience, for many beginners, the paging function is still difficult to achieve, in order to help you solve the problem: the five ring song. From this paging function is like small Yeu Yue singing as concise, beautiful, capable. Haha, no chatter. Formally into the thousand-front php five-ring song of the steps to achieve paging function.
First ring: Calculate the total number of records-that is, the total number of data to display
The second ring: The number of bars displayed per page-that is, our program is usually to specify that each page shows a few data.
Third ring: Calculate the total number of pages-that is, according to the above two conditions to find the total number of pages, in order to prevent the occurrence of decimals, we are rounding up. See the following code for details:
Fourth ring: Shows the current number of pages-that is, we typically need to pass a paging parameter in the address bar, such as p=5, 5 is the current page number.
Fifth ring: Calculate offset-This is the most critical step, the above step is to find the offset of the cushion. Offset = (Current page number-1) * Number of bars displayed per page

Here is an example from my sister:
The above linked database section omitted:
How many data does the query data have in total?

$sql = "SELECT count () from test"; $result = Mysqli_query ($con, $sql); $count = Mysqli_fetch_assoc ($result); $count = Intval ( $count [' count () ']);

Specify a few data per page

$perPage = 5;

Calculates the total number of pages and takes up the entire

$pages = Ceil ($count/$perPage);

Displays the current number of pages

$curPage = isset ($_get[' P ')? Min (max ((int) $_get[' P '],1), $pages): 1;

Finally, the offset is calculated

$offset = ($curPage-1) * $perPage;

Finally, the query data uses limit to display more than one data per page.

$sql = "SELECT * FROM Test ORDER by ID DESC LIMIT $offset,     $perPage"; $result = Mysqli_query ($con, $sql);

Write code like the Thousand-front PHP slogan: Silly x-like persistence, to have the results of the Ox x.

Original link

The five rings of the song of PHP pagination

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.