Reputation: 179
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!
Upvotes: 1
Views: 2298
Reputation: 141
For someone that has never done anything like this it doesn't hurt to learn.
In order to do this you are going to need to pass values through the url and have your sql script change based on the limit value passed in the url.
Things to consider: SQL limit - This is the amount of results you will receive. Page number - This is the page of results that are returned. Sort - This is how the results are going to be returned in the pagination. page links - The list of pages. This is found by taking your result number, dividing by the limit and flooring the number.
The hard part about creating pagination is making it extendible and able to be used with other lists of information such as comments or user lists.
Upvotes: 2
Reputation: 245
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.
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.
Upvotes: 13
Reputation: 21
Are your filenames coming from a database? If so, check out OFFSET and LIMIT filters in your queries. If not, try counting up the number of files you have, divide them out into blocks, and then when you go to the next page let it know where to start.
Here's a hint,
print "<a href='page.php?offset=$offest&limit=$limit'> Next </a>"
It is better for you to try and write this on your own, pagination is a basic feature in PHP applications.
Upvotes: 2
Reputation: 764
Firstly you could try to write your own. Alternatively you could use Smarty (a very well known PHP presentation framework) along with this pagination plugin.
Upvotes: 1
Reputation: 16656
Maybe you try write one? This is very easy thing to write and you will not linked to someone code who can be writed in not good quality or some other else code or framework who can by deprecated in some time.
I know this is not solution for this question but this is a my first idea reading this topic.
Upvotes: 1