https://github.com/php/php-src
In this tutorial, we will learn how to create pagination with PHP OOP system. by using OOP we will create many Classes and functions that will make this pagination system to be dynamic and does not apply to certain data only.
I will create a project folder with the name pagination and create a folder inside. The folder name is class and create a file index.php. I will also create a database to use to create pagination.
Create database
The database name is faker and its table name is users. I will use faker to get dummy data. You can use dummy data for your database, please visit https://github.com/fzaninotto/Faker.
The structure of table users
| Name | Type | Length/value | Indeks |
|---|---|---|---|
| id | INT | 11 | Primary |
| username | Varchar | 100 | -- |
| city | Varchar | 100 | -- |
| Varchar | 100 | -- |
pagination{} classWe will create the class Pagination{} and in the Pagination class, we will create some variables whose contents will often be used.
Example:
<?php
class Pagination{
private $db, $table, $total_records, $limit = 5;
}
?>
__construct (){} function<?php
class Pagination{
private $db, $table, $total_records, $limit = 5;
//PDO connection
public function __construct($table){
$this->table = $table;
$this->db = new PDO("mysql:host=localhost; dbname=faker", "root", "root");
$this->set_total_records();
}
}
?>
__construct(){}. __construct() will be run the first time the function is created. I will explain what will be run in the function __construct ():$table with the value we get from the parameters in the function __contruct ($table).new PDO () method and we can save these connections into variables that have been declared above $this->db.
new PDO("databaseDriver:host=localhost; dbname=databaseName", "username", "password");
3. Call function set_total_records (): We need to get the amount of data in the database for us to use as a reference number of pages to be displayed. function set_total_records () is created outside the function __construct ().
set_total_records(){} function<?php
class Pagination{
public function set_total_records(){
$stmt = $this->db->prepare("SELECT id FROM $this->table");
$stmt->execute();
$this->total_records = $stmt->rowCount();
}
}
?>
prepare () and to run the query we can use execute (). I made a simple query "SELECT id FROM $this->table" to retrieve the id from the 'users' table and do count with rowCount (). $this->table is a variable that has been assigned a value in the function __construct() and then we save the result of the sum in the variable $total_records.Noted: We can access the variables inside the function by using $this.
current_page(){} function?page=pageNumber. We can detect via isset() method and then we can do logic to make the value 1 as a minimum number of pages. statement ? if true : if false. This function only returns the value of the page. This function returns value of the page we will use in the get_data()Example:
public function current_page(){
return isset($_GET['page']) ? (int)$_GET['page'] :1;
}
get_data(){} functionget_data ().Example:
public function get_data(){
$start = 0;
if($this->current_page() > 1){
$start = ($this->current_page() * $this->limit) - $this->limit;
}
$stmt = $this->db->prepare("SELECT * FROM $this->table LIMIT $start, $this->limit");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
We will check if the current page is larger than > 1. We can get the value of current page via $this->current_page(). if $this->current_page() > 1 then we will do the math operation ($this->current_page() * $this->limit) - $this->limit; and We will store the value in $start as a reference starting from the order in which we get the data in the database..
Then we can do the a query with SELECT * FROM $this->table LIMIT $start, $this->limit. it's means SELECT * FROM tableName LIMIT startFrom, limit
And we can return data with object type fetchAll(PDO::FETCH_OBJ)
Create get_pagination_number(){} function
to display the index page. we can create the following function:
Example:
public function get_pagination_number(){
return ceil($this->total_records / $this->limit);
}
$this->total_records and the limit $this->limit. $this->total_records is a variable whose value has been assigned when the __construct () function is executed. $this->limit is a variable that we have assigned with value 5.pagination{} classWe have made the functions in the pagination class, now we will see the usefulness of each of these functions.
get_data ()We have finished creating the pagination{} class. Now we can already use it in index.php.
<?php
require_once 'class/Pagination.php';
$pagination = new Pagination('users');
$users = $pagination->get_data();
var_dump($users);
?>
We import the pagination class with require_once 'class/Pagination.php'. adjust to your file directory.
We initialize the new class on the $pagination variable and we passed the 'users' parameter. This parameter will be accepted by __construct () and used as table name. adjust the table name in your database with this parameter.
We have created get_data () function in the previous section. We have initialized the class pagination inside the $pagination variable.Now we can use it like this $pagination->get_data(). then we save the result in variable $users and I will var_dump() to see the data in browser.
The Result
$users in list HTML. Because $users is an array we can do foreach() to extract the data.Example:
<?php
require_once 'class/Pagination.php'
$pagination = new Pagination('users');
$users = $pagination->get_data();
?>
<!DOCTYPE html>
<html>
<head>
<title>Pagination PDO Class</title>
</head>
<body>
<ul>
<? foreach ($users as $user): ?>
<li><? echo $user->username. ':' .$user->email;?> </li>
<? endforeach; ?>
</ul>
</body>
</html>
$users as $user. now we can expend the value with $user-> key. We can use : and endforeach; to replace {}.We can see in the picture, the parameter query we do successfully process in the database. so when we want to open the third page. we can pass ?page=3 in the browser URL.
get_pagination_numbers()pagination {}, here's how to use it:Example:
<?php
require_once 'class/Pagination.php';
$pagination = new Pagination('users');
$users = $pagination->get_data();
$pages = $pagination->get_pagination_numbers();
?>
get_pagination_numbers () function is the number of pages. We can do for() to sort it.Example:
<?php
require_once 'class/Pagination.php'
$pagination = new Pagination('users');
$users = $pagination->get_data();
$pages = $pagination->get_pagination_numbers();
?>
<!DOCTYPE html>
<html>
<head>
<title>Pagination PDO Class</title>
</head>
<body>
<ul>
<? foreach ($users as $user): ?>
<li><? echo $user->username. ':' .$user->email;?> </li>
<? endforeach; ?>
</ul>
<hr>
<? for($=i; $i<=$pages; $i++): ?>
<a href="?page="><? echo $i;</a>
<? endfor; ?>
</body>
</html>
We do for the$pages variable that contains the number of pages.We can use : and endforeach; to replace {}.
The result
Thank you for following this tutorial, in the next section I will create a better user interface on the page number.