CodeIgniter 4 Pagination Example Tutorial

Apr 23, 2022 . Admin



Hello Friends,

This article will provide example of codeigniter 4 pagination example tutorial. you can understand a concept of pagination in codeigniter step by step. if you have question about pagination in codeigniter example then I will give simple example with solution. if you have question about pagination code in codeigniter then I will give simple example with solution. Follow bellow tutorial step of codeigniter bootstrap 4 pagination.

I will learn you how to use paginaton in codeigniter 4.you can easy and simply use pagination in codeigniter 4.

So let's start the example..

Step 1: Install Codeigniter 4

This is optional; however, if you have not created the codeigniter app, then you may go ahead and execute the below command:

composer create-project codeigniter4/appstarter ci-news
Step 2: Basic Configurations

Next, we will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

app/config/app.php
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';
Step 3: Create Database With Table

In this third step, we need to create a database name demo, so let’s open your PHPMyAdmin and create the database with the name demo. After successfully create a database, you can use the below SQL query for creating a table in your database

CREATE TABLE users (
    id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    name varchar(100) NOT NULL COMMENT 'Name',
    email varchar(255) NOT NULL COMMENT 'Email Address',
    contact_no varchar(50) NOT NULL COMMENT 'Contact No',
    created_at varchar(20) NOT NULL COMMENT 'Created date',
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;
 
INSERT INTO users(id, name, email, mobile_number, created_at) VALUES
  (1, 'Team', 'info@test.com', '9000000001', '2019-01-01'),
  (2, 'Admin', 'admin@test.com', '9000000002', '2019-01-02'),
  (3, 'User', 'user@test.com', '9000000003', '2019-01-03'),
  (4, 'Editor', 'editor@test.com', '9000000004', '2019-01-04'),
  (5, 'Writer', 'writer@test.com', '9000000005', '2019-01-05'),
  (6, 'Contact', 'contact@test.com', '9000000006', '2019-01-06'),
  (7, 'Manager', 'manager@test.com', '9000000007', '2019-01-07'),
  (8, 'John', 'john@test.com', '9000000055', '2019-01-08'),
  (9, 'Merry', 'merry@test.com', '9000000088', '2019-01-09'),
  (10, 'Keliv', 'kelvin@test.com', '9000550088', '2019-01-10'),
  (11, 'Herry', 'herry@test.com', '9050550088', '2019-01-11'),
  (12, 'Mark', 'mark@test.com', '9050550998', '2019-01-12');
Step 4: Setup Database Credentials

In this step, we need to connect our project to the database. we need to go app/Config/Database.php and open database.php file in text editor. After opening the file in a text editor, We need to set up database credentials in this file like below.

public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'demo',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];
Step 5: Create Model

In this step we here to go app/Models/ and create here one model. And you require to create one model name UserModel.php and update the following code into your UserModel.php file:

app/Models/UserModel.php
<?php 
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
 
class UserModel extends Model
{
    protected $table = 'contacts';
 
    protected $allowedFields = ['name', 'email'];
}
Step 6: Create Controller

Now Go to app/Controllers and create a controller name Users.php. In this controller, we will create some method/function. We will build some of the methods like :

app/Controllers/Users.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
 
class Users extends Controller
{

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {    
        $model = new UserModel();
 
        $data = [
            'users' => $model->paginate(10),
            'pager' => $model->pager
        ];
        
        return view('users', $data);
    }
}
Step 7: Create Views

Now we need to create users.php, go to application/views/ folder and create users.php file. and update the following HTML into your files:

application/views/users.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Codeigniter 4 Pagination Example - Mywebtuts.com</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
 
    <div class="container">
        <div class="row mt-5">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if($users): ?>
                    <?php foreach($users as $user): ?>
                    <tr>
                        <td><?php echo $user['id']; ?></td>
                        <td><?php echo $user['name']; ?></td>
                        <td><?php echo $user['email']; ?></td>
                    </tr>
                   <?php endforeach; ?>
                   <?php endif; ?>
                </tbody>
            </table>
            <div class="row">
                <div class="col-md-12">
                    <div class="row">
                        <?php if ($pager) :?>
                        <?php $pagi_path='demo/public/index.php/users'; ?>
                        <?php $pager->setPath($pagi_path); ?>
                        <?= $pager->links() ?>
                        <?php endif ?>        
                    </div> 
                </div>
            </div>
        </div>
    </div>
</div> 
</body>
</html>
Step 8 : Run Codeigniter App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Codeigniter app:

php spark serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8080/

i hope it will help you...

#Codeigniter 4