CodeIgniter Forums
How to create pagination on CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to create pagination on CI4 (/showthread.php?tid=76032)

Pages: 1 2


How to create pagination on CI4 - Marcolino92 - 04-08-2020

Hi, I'm having a lot of trouble figuring out how to create pagination on Codeigniter 4.

This is my controller:

PHP Code:
$model = new TestModel();
        
        $data 
= [
            'website' => lang('Auth.website'),
            'title' => lang('Auth.title_home'),

            'auth' => new \App\Libraries\IonAuth(),
            'model' => new TestModel(),

            'validation' => \Config\Services::validation(),
            'session' => \Config\Services::session(),
            'pager' => \Config\Services::pager(),
            
            
'test' => $model->getItem(),
            'categories' => $model->categoryList(),
            'lastComments' => $model->lastComments(5),
        ]; 


This is the model:


PHP Code:
public function getItem($slug false){
        
        
if($slug === false){
            return $this->query('SELECT * FROMa items ORDER BY id DESC')
                ->getResult();
        }

        return $this->asArray()
            ->where(['slug' => $slug])
            ->first();
    


While in the view I recall the pagination. As you can see in the model I create the query to retrieve the results, while in the controller I call the model and pager. But I don't know what else to do, I don't understand how and why to use paginate ()


RE: How to create pagination on CI4 - Marcolino92 - 04-09-2020

Can someone help me? I just can't understand the functionality of paginate ()


RE: How to create pagination on CI4 - jouharjaseemak - 04-09-2020

$pager = \Config\Services::pager();

if($this->request->uri->getTotalSegments() == 4)
{
$page = $this->request->uri->getSegment(4);
}
else
{
$page = 1;
}
$perPage = 2;
$data['pages'] = $pager->makeLinks(1, $perPage, 20,'front_full');

echo $pages in your view like <?=$pages?>


RE: How to create pagination on CI4 - wdeda - 04-09-2020

Here you find the basics:

https://codeigniter.com/user_guide/libraries/pagination.html?

The Model is not displayed but it could look like this:

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UserModel extends Model
{
    protected $table  'users';
    protected $primaryKey 'id';    
    
protected $allowedFields = ['id''name''email'];    

}

?>

I am trying not CRUD but the pagination links "$pager->links()" is not working and even changing it manually, in the browser navigation bar, the same page is always displayed.


RE: How to create pagination on CI4 - Marcolino92 - 04-09-2020

How can I order the results from the most recent?


RE: How to create pagination on CI4 - wdeda - 04-09-2020

Try this:
https://codeigniter.com/user_guide/models/model.html


RE: How to create pagination on CI4 - Marcolino92 - 04-09-2020

I was unable to find a solution. Since paginate () automatically fetches the results from the main table, I cannot act on a query in order to add orderBy.


RE: How to create pagination on CI4 - wdeda - 04-09-2020

My site is local, catalogs of films and records, it was developed with CI3, no problem so far, currently I only update the tables.

Out of curiosity I tried to use CI4 using the same database but I am about to give up, like you I am stuck in the pagination.
I think that something is only automated when you have mastered the manual, but I don't, or at least there is something saying how to do it. I am aware of my limitations, which is certainly the reason why I am unable to go further.
It's a hobby for me, CI3 meets my needs, who knows later.
I am on the edge of the abyss ... about to fly.

I'm sorry, I did my best.


RE: How to create pagination on CI4 - jouharjaseemak - 04-09-2020

finaly i have find a solution...any one want it ?


RE: How to create pagination on CI4 - Marcolino92 - 04-10-2020

(04-09-2020, 01:01 PM)wdeda Wrote: My site is local, catalogs of films and records, it was developed with CI3, no problem so far, currently I only update the tables.

Out of curiosity I tried to use CI4 using the same database but I am about to give up, like you I am stuck in the pagination.
I think that something is only automated when you have mastered the manual, but I don't, or at least there is something saying how to do it. I am aware of my limitations, which is certainly the reason why I am unable to go further.
It's a hobby for me, CI3 meets my needs, who knows later.
I am on the edge of the abyss ... about to fly.

I'm sorry, I did my best.

Until a few weeks ago, I also did with CI3, but with the new release of version 4 it is always better to update. There is to learn and making many mistakes, thanks anyway.