
- Views: 5.2K
- Category: Codeigniter
- Published at: 29 Oct, 2020
- Updated at: 12 Aug, 2023
Pagination in Codeigniter 4 | Pagination Service | Pagination library
Pagination in Codeigniter 4 | Pagination Service | Pagination library
What's Pagination, huh? If you have tons of material/posts to view on a single page, it's challenging to describe thousands of blogs/products on a single page, so you're eventually required to break them into several pages.
Have you ever created the Pagination in Core/Basic PHP or even Codeigniter 3? Many PHP scripts are required to create the Pagination in core PHP. Still, if you talk about the Codeigniter 3 pagination, a few lines of code are required to create it, maybe ten lives of code.
Creating the Pagination in Codeigniter 4 is very easy; you need to call a method sounds good. Let me describe the Pagination in Codeigniter 4.
Create a table named users
create table users
(
u_id int auto_increment
primary key,
u_name varchar(250) not null,
u_user_name varchar(250) null,
u_email varchar(250) not null,
u_password varchar(250) not null,
u_link varchar(250) null,
u_contact_number varchar(30) null,
u_dp varchar(250) null,
country_id int not null,
city_id int not null,
u_description text null,
u_verify_batch int default 0 null,
u_status int default 0 null,
u_date datetime null,
u_updated datetime null,
u_deleted datetime null,
u_agree int default 0 null comment 'agree terms and condition while creating the new account.',
u_deals_in varchar(50) null,
u_working_since varchar(50) null,
u_slug varchar(250) null,
u_address text null,
admin_id int null
)
collate=latin1_swedish_ci;
Now create a model named ModUsers in Codeigniter 4
<?php namespace App\Models;
use CodeIgniter\Model;
class ModUsers extends Model
{
protected $DBGroup = 'default';
protected $table = 'users';
protected $primaryKey = 'u_id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = [
'u_name','u_user_name',
'u_email','u_password', 'u_link', 'u_contact_number','u_dp',
'country_id','city_id', 'u_description', 'u_verify_batch','u_status',
'u_date','u_updated', 'u_deleted','u_agree','u_deals_in','u_working_since','u_slug','u_address','admin_id'
];
protected $useTimestamps = true;
protected $createdField = 'u_date';
protected $updatedField = 'u_updated';
protected $deletedField = 'u_deleted';
protected $validationRules = [
'u_name' => 'required',
'u_email' => 'required|valid_email',
'u_password' => 'required|min_length[5]',
'u_user_name' => 'required',
'u_contact_number' => 'required',
'country_id' => 'required',
'city_id' => 'required',
];
protected $validationMessages = [
];
protected $skipValidation = false;
}//class here
Create a controller named Users in Codeigniter 4
<?php
namespace App\Controllers;
use App\Models\ModUsers;
class User extends BaseController
{
$userTable = new ModUsers();
$data = [
'companies' => $userTable->paginate(20),
'pager' => $userTable->pager
];
echo view('home',$data);
}
Note You have to create the view named home to send the data; that's it.
As you know, we have used only two methods to create the pagination and also the links one is the paginate, and the second method is a pager.
To create the links on your view, you need to call the below method.
<?php echo $pager->links();?>
Conclusion
Pagination is a way of displaying content on a single page, and it allows you to display content in a variety of ways. In Codeigniter 4, you need to call a method to create the pagination. The PPaginationcan also be created in Core/Basic PHP.
https://www.youtube.com/watch?v=mSNcs-O-MqM
0 Comment(s)