Welcome Guest, Not a member yet? Register   Sign In
CI 4 pagination problem
#2

I also had problems at first and I ended up doing this.

The code should give you an idea of how to do it, you will notice I am returning $this
so that we can use method chaining.

Don't worry about the group stuff it's because I have custom bootstrap pager templates.

PHP Code:
    // CONTROLLER
    
    
<?php namespace App\Controllers;

use 
App\Models\AuthorModel;
use 
Config\Services;

/**
 * Class Home
 *
 * @package App\Controllers
 */
class Home extends BaseController
{
    private 
$author;

    public function 
index()
    {
        
$pager Services::pager();

        
$this->author = new AuthorModel();
        
        
// This will get all Authors
        
$data = [
            
'authors' => $this->author->getAllAuthors()->paginate(4'group1'),
            
'pager'   => $this->author->pager,
        ];

        echo 
view('test'$data);

        
// This will get all authors by category id
        
$dat = [
            
'authors' => $this->author->getAuthorsByCategory(14)->paginate(4'group1'),
            
'pager'   => $this->author->pager,
        ];

        
//echo view('test', $dat);

        //return view('welcome_message');

    
}

    
//--------------------------------------------------------------------

}
    
// MODEL
    
    // -------------------------------------------------------------------

    /**
     * getAuthors ()
     * -------------------------------------------------------------------
     *
     * Get all authors
     */
    
public function getAuthors()
    {
        
$builder $this->builder();

        
$builder->orderBy('id''ASC');
        
$builder->get();
        
        return 
$this;
    }

    
// -------------------------------------------------------------------

    /**
     * getAuthorsByCategory ()
     * -------------------------------------------------------------------
     *
     * Get all authors by category
     */
    
public function getAuthorsByCategory(int $id)
    {
        
$builder $this->builder();

        
$builder->where('category_id'$id);

        return 
$this;
    }

    
//--------------------------------------------------------------------

    // VIEW
    
    
<!DOCTYPE html>
<
html lang="en">

<
head>

    <
meta charset="UTF-8">

    <
title>Title</title>

    <
link href="<?= base_url('assets/vendor/bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">

</
head>

<
body>

    <
div class="container-fluid">
    <?
php

    
foreach ($authors as $key => $list)
    {
        
//echo $key."<br>";
        
echo $list['id']."<br>";
        echo 
$list['category_id']."<br>";
        echo 
$list['first_name']."<br>";
        echo 
$list['last_name']."<br>";
        echo 
$list['email']."<br>";
        echo 
$list['birthdate']."<br>";
        echo 
$list['added']."<br><br>";
    }

    echo 
$pager->simpleLinks('group1''bs_simple');

    
//echo $pager->makeLinks($page, $perPage, $total, 'bs_simple');

    
?>

    </div>

    <script href="<?= base_url('assets/vendor/jquery/jquery-3.5.1.min.js');?>"></script>
    <script href="<?= base_url('assets/vendor/bootstrap/js/bootstrap.bundle.min.js');?>"></script>

</body>
</html> 

This works like a charm for me.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
CI 4 pagination problem - by lukasz.bluedragon - 09-20-2020, 05:29 AM
RE: CI 4 pagination problem - by InsiteFX - 09-20-2020, 10:16 PM
RE: CI 4 pagination problem - by InsiteFX - 09-22-2020, 05:35 PM
RE: CI 4 pagination problem - by nc03061981 - 09-23-2020, 01:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB