Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand PHP pagination

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand PHP pagination". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

Paging display is a very common way to browse and display large amounts of data, and it is one of the most common events handled in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this problem, so I wrote this article to explain this problem in detail, and strive to make friends who finish reading this article understand the principle and implementation of paging display. This article is suitable for beginners, and all sample code is written in php.

2. Principle

The so-called paging display is to artificially divide the result set in the database into segments to display, here you need two initial parameters:

How many records per page ($PageSize)?

What is the current page ($CurrentPageID)?

Now just give me another result set and I can display a specific result.

As for other parameters, such as the previous page ($PReviousPageID), the next page ($NextPageID), the total number of pages ($numPages), etc., can be obtained from the previous few things.

Take the MySQL database as an example. If you want to intercept a section of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of sql statements and try to find the rules in it.

The first 10 records: select * from table limit 0pm 10

Records 11 to 20: select * from table limit 10

Records 21 to 30: select * from table limit 20jin10

……

This set of sql statements is actually a sql statement that fetches each page of data in the table when $PageSize=10. We can sum up a template like this:

Select * from table limit ($CurrentPageID-1) * $PageSize, $PageSize

Compare this template with the corresponding value with the above set of sql statements to see if that's the case. Once you've solved the most important question of how to get the data, all that's left is to pass the parameters, construct the appropriate sql statement, and then use php to get the data from the database and display it. I will explain it with specific code below.

3. Simple code

Please read the following code carefully, debug and run it yourself, it is best to modify it once, plus your own functions, such as search and so on.

The copy code is as follows:

4. OO style code

The database connection in the following code is handled using the pear db class

The copy code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report