Pagination based on Arrays in PHP
We know that pagination is the way through which we can display all our data in a single page. It denotes that the data is fetched from database based on the limit. For example, I want to display the data from product table in database. There are 20000 data entries but we can display only 5 data on a single page. That would mean we need 4000 pages. This can be solved using pagination. I came across several types of pagination while searching. But I didn’t need extensive database interaction that most of the techniques required. So, I narrowed down on a concept of pagination based on arrays, which a friend of mine uses. He gave me the code and I customized it and used in my work. This code needs only the record array that comes from the SQL query. It holds data and displays as per requirement. Use it and forget about the extensive database interaction normally used in pagination code. I hope you find this post useful.
You will have 3 files
Paginated.php:-
It is the main class to be used in pagination. Create object of this class and pass the recordset array.
Here in this Object declaration, there are three parameters we need to pass .the 1st parameter is the recordset array,2nd is no of record will be displayed in a single page and 3rd is the page no that gives the next page from URL.
After that, use a while loop to display the data in one page. Here is an example :
displays data here from array $r[‘name’]
DoubleBarLayout.php
This file is used to design or customize your pagination numbers first page, last page, next page, previous page etc.
PageLayout.php
This file is used for the interface which specifies the behavior of all page layouts that the classes must implement
A customized file is attached with this post. Please feel free to use the file, and do give your comments by replying to this post.
File Link:- http://blog.codez.in/post-files/paginationArrayClass.zip
Latest posts by Koushik Nandy
- Tag Cloud Creation in PHP - October 1st, 2010
- Websites and their coding security - July 26th, 2010
- Basic PHP Security Checklist - July 16th, 2010
- PHP5 time zone solution - July 9th, 2010
- Converting XML to HTML - July 8th, 2010
