Getting started with pagination
Remarks#
This section provides an overview of what pagination is, and why a developer might want to use it.
It should also mention any large subjects within pagination, and link out to the related topics. Since the Documentation for pagination is new, you may need to create initial versions of those related topics.
Installation or Setup
Detailed instructions on getting pagination set up or installed.
Pagination solution
The following documentation describes both MySQLi and PDO supported pagination solution.
Go to https://github.com/rajdeeppaul/Pagination and download pagination.php
file into your project directory. Let’s say your directory structure looks like this:
project directory
|
|--pagination.php (pagination script)
|--index.php (file where you want to use this pagination script)
And suppose you want the URL to look like this:
https://example.com/index.php // user is on page 1
https://example.com/index.php?page=1 // user is on page 1
https://example.com/index.php?page=5 // user is on page 5
// etc ...
index.php
-
Include
pagination.php
file inindex.php
page, like this:require_once('pagination.php');
-
Create an instance of
Pagination
class, like this:$pg = new Pagination($databaseDriver, $hostname, $username, $password, $databaseName);
The constructor method takes the following parameters,
(1)
$databaseDriver
: Database driver, currently supported drivers are MySQLi and PDO
(2)$hostname
: Hostname
(3)$username
: Username
(4)$password
: Password
(4)$databaseName
: Database nameExample(s):
$pg = new Pagination('mysqli', 'localhost', 'root', 'pass', 'pagination_db'); $pg = new Pagination('pdo', 'localhost', 'root', 'pass', 'pagination_db');
-
Set pagination parameters using
setPaginationParameters()
method, like this:$pg->setPaginationParameters($rowsPerPage, $numOfPaginationLinks);
The
setPaginationParameters()
method takes the following parameters,(1)
$rowsPerPage
: Number of table rows to display per page
(2)$numOfPaginationLinks
: Number of pagination links to display per pageExample(s):
$pg->setPaginationParameters(10, 5);
-
Call
getResult()
method to display table rows based on the URL query?page=X
, like this:$resultSet = $pg->getResult($queryString, $bindParameterArray, $globalGetArray, $keyFromURLQuery);
The
getResult()
method takes the following parameters,(1)
$queryString
:SELECT
query string
(2)$bindParameterArray
: Array containing bind variables or values
(3)$globalGetArray
: Superglobal array$_GET
(4)$keyFromURLQuery
: Key from the URL query i.e.page
Example(s):
$resultSet = $pg->getResult('SELECT * FROM pagination', NULL, $_GET, 'page'); $resultSet = $pg->getResult('SELECT * FROM pagination WHERE column1 = ? AND column2 = ?', array($value1, $value2), $_GET, 'page');
Note: Don’t specify any
LIMIT
orOFFSET
clause in the query, the script will take care of these.Now loop through the result set i.e.
$resultSet
array to access/display row details, like this:foreach($resultSet as $row){ /* access/display row details */ /* $row['column1'], $row['column2'] etc. */ }
Note: If you want to see the complete array structure, do
var_dump($resultSet);
. -
Display pagination links using
getPaginationLinks()
method, like this:$pgLinks = $pg->getPaginationLinks();
The
getPaginationLinks()
method doesn’t take any parameter and returns an array of pagination links in the following format,array (size=3) 'prev' => @boolean 'links' => @array 'next' => @boolean
Now loop through the
$pgLinks
array to display paginagtion links, like this:if(is_array($pgLinks) && count($pgLinks) && $pgLinks['prev']){ /* previous pages are available */ echo '« '; } if(is_array($pgLinks) && count($pgLinks) && count($pgLinks['links'])){ /* show pagination links */ foreach($pgLinks['links'] as $link){ echo '<a href="example.php?page='.$link.'">'.$link.'</a> '; } } if(is_array($pgLinks) && count($pgLinks) && $pgLinks['next']){ /* next pages are available */ echo '»'; }
Note: If you want to see the complete array structure, do
var_dump($pgLinks);
.
Footnote(s): You can style the result rows and pagination links as per your choice.