Paginate a Pagination component for PHP
This component has two files.
It is used in the controller (if speaking MVC terms) and sets up some
convinience methods for you to use.
It is used to render a pagination bar.
NOTE: this component currently relies on MySQL, so remember to set up your
connections before using it in your code!
You have to tell the component what page we are currently on and what table
your data is to be taken from. This is “all” you need to do.
For simplisity, i’ll just use a single file called index.php
// make your connection to the database
// initializes the pagination lib
$paginate = new Paginate(
“posts”, // the table name in your database to find ALL items from
$current_page, // the current page
$items_per_page // number of items per page
// use the paginate view helper to make the pagination bar
$paginateViewHelper = new PaginateViewHelper(
$paginate→getCurrentPage(), // the current page we are on, e.g. 1
$paginate→getItemsPerPage(), // number of items per page, e.g. 5
“/blog/page/” // the link to have before the page number
// renders the pagination bar. Only needs to know to total amount of pages
// this can be taken from the $paginate object.