• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create pagination on CI4

#1
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 ()
Reply

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

#3
$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?>
Reply

#4
Photo 
Here you find the basics:

https://codeigniter.com/user_guide/libra...ation.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.


Attached Files Thumbnail(s)
       
Reply

#5
How can I order the results from the most recent?
Reply

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

#7
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.
Reply

#8
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.
Reply

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

#10
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.