Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

Pagination in PHP

I have a page that serves a list of files for users to download. There can be hundreds of files there, and I would like to break them down into multiple pages and give users the option either to see them page by page or get them all displayed in one page. I've never done anything like this and don't know how to do it.

Please help. Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, I had this question too once. This is basically the logic behind the pagination, I am not going to write code but if you need it let us know.

Basically, you need two values to create a pagination, a limit and a offset.

  • The limit is the amount of items your are displaying at the same time.
  • The offset is from where you started your query.

So, let's say you have 5 items in each page and 25 items total.

In your query, you have to limit 5,0 (the amount of items and the position the query will start).

Now, if you divide 5(limit)/25(total) and you'll get 5 (amount of pages).

Now in page 0 (the start) you can get the offset by multiplying the page number with the limit, so 0 (page) * 5 (limit) gives you 0 (in the first page you start from the offset 0).

Now in the 3rd page, you multiply 3(page) * 5 (limit) it gives you 15, which means in page 3 (or four if you take into account that you actually started at page 0) you will display from offset 16 to 20.

Finally in page 4 (which to your users will be page 5 because they started at page 1, not page 0) you will display from offset 21 to 25 which are all the items in your query.

I hope after reading this you understand the logic behind pagination, if you need help with the code, again, just let us know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...