Pagination with a PHP class
I’ve recently wrote a pagination class for php, I used to do this in a procedural way by using functions, it’s always worked fine, but its not as reusable as a class is.
I’ve released this class on Github this tutorial will explain how to use the pagination class.
- Include the class
- create a new instance of the class, set the number of records by page and a reference
- Then set the total number of records, do this by either setting in manually or by counting records from a dataset
- Set the total number of records by passing the total to the method set_total()
- Then display the records as normal
- To show the page links echo page_links() optionally passin 2 parameters the first set the address by default this is ? the second parameter is for any $_GET’s you want to pass from page to page.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//include the class include('paginator.php'); //create new object pass in number of pages and identifier $pages = new Paginator('10','p'); //get number of total records $rows = $db->query('SELECT id FROM table'); $total = count($rows); //pass number of records to $pages->set_total($total); $data = $db->query('SELECT * FROM table '.$pages->get_limit()); foreach($data as $row) { //display the records here } //create the page links echo $pages->page_links(); |
Download the class from Github
-
http://www.biztechconsultancy.co.uk/php-web-development.htm PHP Web Development
-
http://www.daveismyname.com David Carr
-
http://www.biztechconsultancy.co.uk/php-web-development.htm PHP Web Development
-
-
-
Guest
-
http://www.daveismyname.com David Carr
-
-
mrashutoshanand