How to implement paging function

In the previous chapter, we completed the modification of the background content display html page. Here we need toquery the database data through SQL statements and display it in the table.The paging function is used here to display. After all, the number displayed on the first page is limited.

The so-called paging display means that the result set in the database is artificially divided into sections for display. Two initial parameters are required here:

Number of displays per page:$limitNews

Get the current number of pages:$page

Because the computer language starts obtaining records from 0 by default

If the number displayed per page is set to 3, $limitNews = 3,

Then the first page $page = 1 will display three records of 0, 1, 2

The second page $ page = 2 will display three records 3, 4, 5

The third page $page = 3 will display three records 6, 7, 8

and so on. . . . . . . . .

Set a parameter $limitFrom from which piece of data to start reading

You can get it from the above rules:

$limitFrom = ($page - 1) * $ limitNews;

will start to obtain data from the 0th, 3rd, and 6th items respectively.

Get the total number of news in the database table through query statements $countNews

Here you need to give another parameter $countPage How many pages are displayed in total

Now Another question arises. If there are 10 news records and 3 records are displayed on each page, what about the remaining one?

We need to use % remainder to make the judgment:

 0) { //获取的页数有余 $countPage = ceil($countNews/$limitNews); // ceil() 函数向上舍入为最接近的整数,除不尽则取整数+1页, 10个新闻每个页面显示3个,成3个页面,剩余1个单独成1个页面,这样总共有4个页面 } else { $countPage = $countNews/$limitNews; //如果是9个新闻每个页面显示3个,成3个页面 } ?>

Other parameters are $prev for the previous page and $next for the next page;

In the paging function There are often clicks on "Previous Page" and "Next Page" to jump

Let's talk about the idea of implementing the PHP code function first:

Previous page $prev is the current page $page -1 jumps to the previous page step by step. When the current page $page is the first page, jumping forward will be page 0. This will obviously cause bugs.

Here we will You need to give it a setting. When the current page $page is the first page, click "Previous Page" to set it to display as the first page and no longer jump forward.

The next page $next is the current page $page -1 and jumps to the next page step by step. The problem is that when $page is the last page, click "Next Page" to jump back. ,

Similar to the previous page, we set it to display the last page.

 $countPage)?$countPage:$page+1; ?>

//When the current page number is greater than the total page number, the current page is displayed.

    分表页 

Friends can add several pieces of test data to the databaselisttable to test the paging function.

Use the while statement to loop out the data in the database and display it in the list.php file:

   " />       首页       

Note: The date function is used here to convert the timestamp, which is displayed as a normal addition. time.

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!