Welcome Guest, Not a member yet? Register   Sign In
How to create pagination on CI4
#11

EDIT:

'test' => $model->orderBy('id', 'DESC')->paginate(2, 'res'),
Reply
#12

(This post was last modified: 04-11-2020, 12:52 AM by wdeda.)

Thank you! Amazing how a simple detail changes everything. As soon as I saw your post the solution to my problem came complete at once.
In addition to the model that collects the table data, I created another one where the table fields are selected according to the search argument.

PHP Code:
<?php namespace App\Controllers;
 
 
//20200410
 
    use CodeIgniter\Controller;
    use App\Models\AlbunsListModel;
 use 
App\Models\AlbunsGenresModel;

    class Albumlists extends Controller {    
        
    
public function genre1()
    {        
        $listModel 
= new AlbunsListModel();
        $genresModel = new AlbunsGenresModel();

        $style $genresModel->getGenre1();
                
        $data 
= [
            'genre1' => $listModel->orderBy('player_id''ano''title''asc')
            ->where('style1'$style)
            ->paginate(20),
            'pager' => $listModel->pager,
            'total' => $genresModel->numGenre1(),
            'genre' => $genresModel->getGenre1()
        ];
        
        $genre 
$genresModel->getGenre1();

        $header = array ( 
 
'icon' => 'favicon'
 
'css' => 'albumlists',
 
'title' => $genre'records. - wdeda',
 
'action' => '/ci4/search/allmedia/',
 
'placeholder' => 'Pesquisar'
 
);

 echo 
view('templates/header'$header);
 echo 
view('content/albums/genre1'$data); 
 echo 
view('templates/footer');
    }
//and so on... 

 There is still a pending detail. In the page header I inform the number of items listed in relation to the total pages:
1->20 of 1426 titles, for example, but I need the segment number instead of the query parameter:

http://localhost/ci4/albumlists/genre1/2384/20 instead http://localhost/ci4/albumlists/genre1/2384?page=2

PHP Code:
<? php
if ($this->uri->segment (4)! = null)
{
$ seg = $this->uri->segment (4);
}else{
$seg = 0;
}
$pg = $seg + 20;
$prv = 1 + $seg;
if ($pg == null)
{
$pg = 20;
$prv = 1;
echo "$prv-$pg of $total title(s).";
}elseif ($pg > $ total)
{
echo "$prv-$total of $total title (s).";
}else{
echo "$prv-$ pg of $total title (s)."; 

The manual has an example how I can use the segment number instead of the query parameter that would resolve the pending issue, but I'm an old man, everything is slow, I still can't understand where the change is made and how.

<?= $ pager->makeLinks($ page, $ perPage, $ total, 'template_name', $ segment)?>

"When you look at an abyss for a long time, the abyss looks at you."
(Nietzsche)

From time to time I will look at this but only a little.
Once again, thank you very much!
Reply
#13

(04-09-2020, 11:00 PM)jouharjaseemak Wrote: finaly i have find a solution...any one want it ?
Yes, Plz share
Reply
#14

Actually the solution is really simple. In your Model, if you have a function that fetches data with specific query, instead of ending it with ->findAll(), you finish it with ->paginate('# of pages',optional 'group name')

something like this:

Model

PHP Code:
public function getMissions($id null)
 {
 if (!
$id){
 return 
$this->select('mission.*, COUNT(subscription.id) as subscribers')
 ->
join('subscription','subscription.id_mission = mission.id','left')
 ->
where('date >=',date('Y-m-d'))
 ->
groupBy('mission.id'
 ->
orderBy('date','ASC')
 ->
paginate(15,'active');
 }
 return 
$this->select('mission.*, COUNT(subscription.id) as subscribers')
 ->
join('subscription','subscription.id_mission = mission.id','left')
 ->
where('date >=',date('Y-m-d'))
 ->
where('mission.id',$id)
 ->
first();
 } 

Controller

PHP Code:
public function index()
 {

        $missions = new MissionModel;
        
        $data
['missions'] = $missions -> getMissions();
        $data['pager']    $missions -> pager;
        
        echo view
('mission/index',$data);

 } 

View

PHP Code:
<?= $pager->simplelinks('active'?>
// or
<?= $pager->links('active'?>
Reply
#15

(This post was last modified: 09-18-2021, 01:07 AM by InsiteFX.)

This is how I do it for my Blog I'm building.

PHP Code:
// post model method

/**
 * getLivePosts ()
 * -------------------------------------------------------------------
 *
 */
public function getLivePosts() : PostModel
{
    $builder $this->builder();

    $builder->where('status''published')
            ->orderBy('created_at''desc');
        
    
// for method chaining
    return $this;
}

// Blog Controller

/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 */
public function posts()
{
    $pager Services::pager();

    $posts      = new PostModel($this->request);
    $categories = new CategoryModel();

    $data = [
        "featured"    => $posts->getFeaturedPost(),
        'posts'      => $posts->getLivePosts()->paginate(4'group1'),
        'pager'      => $posts->pager,
        'currentPage' => $posts->pager->getCurrentPage('group1'),
        'totalPages'  => $posts->pager->getPageCount('group1'),
        'categories'  => $categories->getTopCategories(),
        'title'      => 'Blog Home',
        'pageHeading' => 'Welcome to our Blog!',
        'subHeading'  => '',
        'typography'  => Services::typography(),
    ];

    echo view('Insitefx\Blog\Views\posts\index');
}

Notic the model returning $this for method chaining
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#16

(This post was last modified: 02-26-2024, 06:21 PM by danger89.)

This no longer works... Because I'm no longer able to pass variables to my own pager PHP page in CI4.

I was also using variables to my view as well:
PHP Code:
            'currentPage' => $myModel->pager->getCurrentPage('group1'), // The current page number
            'totalPages'  => $myModel->pager->getPageCount('group1'),  // The total page count 
With the latest CI4 version, this will give you an error message when trying to use this in your pager PHP code:

Quote:Undefined variable $currentPage

To answer my own question. It's solved by calling the methods in the template....


PHP Code:
    <button type="button" class="btn" disabled><?= 'Pagina ' $pager->getCurrentPageNumber(). ' of the ' $pager->getPageCount(); ?></button> 


Keep in mind that you notice also the get methods are changed to getCurrentPageNumber(). And I no longer supply the group.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB