Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
Home » Codeigniter 4 »
How to use Pagination in CI4?
Table
File name : index.php
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, 'Mahtab', 'mahtab@test.com', '9000000001', '2019-01-01'),
(2, 'Alam', 'alam@test.com', '9000000002', '2019-01-02'),
(3, 'Habib', 'habib@test.com', '9000000003', '2019-01-03'),
(4, 'Kalam', 'kalam@test.com', '9000000004', '2019-01-04'),
(5, 'Sara', 'sara@test.com', '9000000005', '2019-01-05'),
(6, 'Arham', 'arham@test.com', '9000000006', '2019-01-06'),
(7, 'Sana', 'sana@test.com', '9000000007', '2019-01-07'),
(8, 'Adiba', 'adiba@test.com', '9000000055', '2019-01-08'),
(9, 'bebs', 'bebs@test.com', '9000000088', '2019-01-09'),
(10, 'Simran', 'simran@test.com', '9000550088', '2019-01-10'),
(11, 'sim', 'sim@test.com', '9050550088', '2019-01-11'),
(12, 'sonam', 'sonam@test.com', '9050550998', '2019-01-12');
Create Model
File name : index.php
<?php
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'contacts';
protected $allowedFields = ['name', 'email'];
}
Controller
File name : index.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\UserModel;
class Users extends Controller
{
public function index()
{
$model = new UserModel();
$data = [
'users' => $model->paginate(10),
'pager' => $model->pager
];
return view('users', $data);
}
}
view
File name : index.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 - Tutsmake.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>
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php