• Call Us: +92-333-7276335
  • - Mail Us: info@shekztech.com

Plot 1177, Sector 31B - Crossing, Karachi, Sindh

Loading...
Pagination in Codeigniter 4 | Pagination Service | Pagination library
  • Views: 4.6K
  • Category: Codeigniter
  • Published at: 30 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

Shehzad Ahmed

Shehzad Ahmed is a highly qualified expert with a Master of Philosophy in Computer Science and a decade of extensive industry experience. With his impressive track record in web development and computer science, he has not only left an indelible mark on the industry but also made substantial contributions to education. Since , he has created more than eighty exhaustive courses, paving the way for innumerable individuals interested in learning and development. His unparalleled knowledge and innate ability to elucidate complex ideas make him a highly sought-after educator and consultant. Choose Shehzad and take advantage of his exceptional balance of technical expertise and teaching prowess to propel your learning journey or project to new heights.

0 Comment(s)
Write your comment