I think creating pager in CodeIgniter 4 is more easy than in CodeIgniter 3. Just need a minutes to make us comfortable to CI4 rules, even more we just migrating from CI3.
CI4 provides features to make pagination automatically, and in this article I want to tell you how to do this. For custom pagination and theme usage we will discuss in another post.
What's interesting about this CI4 pagination library, besides its simple and easy to manage the templates, it also supports multipagination on one page. So, for example if we want to make multiple paginations in one view, it can be possible with the grouping pager.
Load The Library
To load the pagination library in CI4, you can do something like this:
$pager = \Config\Services::pager();
Creating pager from database result
One more interesting thing from this CI4 pager library is the paginate() function that we can use to make pagination from the database result. We don't need to bother making settings, we just need to add existing functions.
To avoid confusion, I give examples, for example, this is a script for pagination on a blog page.
use App\Models\BlogModel;
class Blog extends BaseController
{
protected $BlogModel;
public function __construct() {
$this->BlogModel = new BlogModel();
}
public function index()
{
$get_blog = $this->BlogModel->paginate(6);
$data['blog'] = $get_blog;
$data['pager'] = $this->BlogModel->pager;
}
echo view('users/index', $data);
}
That is a example of controller that you can use. I call BlogModel as usual, you can use other methods, as comfortable as you.
Then, focus on this line:
$get_blog = $this->BlogModel->paginate(6);
there is no paginate function in BlogModel, the paginate function is built in to the CI4 model system. If you want to check, you can check on system/Model.php, you will find scripts like:
public function paginate(int $perPage = 20, string $group = 'default', int $page = 0)
{
$pager = \Config\Services::pager(null, null, false);
$page = $page >= 1 ? $page : $pager->getCurrentPage($group);
$total = $this->countAllResults(false);
// Store it in the Pager library so it can be
// paginated in the views.
$this->pager = $pager->store($group, $page, $perPage, $total);
$offset = ($page - 1) * $perPage;
return $this->findAll($perPage, $offset);
}
But just ignore it, just for knowledge. So we know how the functions in the framework can work.
The model that I used (after I simplified it) became like this:
use CodeIgniter\Model;
class BlogModel extends Model
{
public function __construct() {
parent::__construct();
$db = \Config\Database::connect();
$this->builder = $db->table('blog');
}
}
There is no function here. I think its not read the function but the table that we defined in construct.
For join and select option we can do also as additional query after the table('blog')->where....
Then, how to print the pager in view? Just like:
links() ?> or simpleLinks() ?>
What's the difference? When using links() it will display the pager numbers, while using simpleLinks will display the order and newer pager.
Actually, we can just change it in the template, according to our taste, we will discuss it in another article. While following this article, you can display pagination in CodeIgniter 4.