Welcome Guest, Not a member yet? Register   Sign In
Pagination don't work
#1

(This post was last modified: 11-15-2019, 02:19 AM by dimonisse.)

Hi all!
I'm trying to configure pagination for my project, I do everything according to the documentation, but I get an error. With version 3, there were no such problems ((

PHP Code:
lass User extends Controller{
    
    
public $model;
    public $locale;

    function __construct()
    {
        $this->locale  =  service('request')->getLocale();
        $this->model = new UserModel();
    

    /*
     * Listing of users
     */
    function index()
    {

        $data = [
            'users'=>$this->model->get_all_users()->paginate(10),
            'pager'=>$this->model->get_all_users()->pager,
            'locale' => $this->locale
        
];

        echo view('admin/header',$data);
        echo view('admin/user/index',$data);
        echo view('admin/footer');
    

Model:
PHP Code:
class UserModel extends Model
{
    protected $db;
    protected $builder;

    function __construct()
    {
        $this->db = \Config\Database::connect();
        $this->builder $this->db->table('users');
    }
  /*
     * Get all users
     */
    function get_all_users()
    {

//        $this->builder->select('id_user, name_user');
        $this->builder->orderBy('id_user''asc');

        return $this->builder->get()->getResultArray();
    
Error:

Quote:Error
Call to a member function paginate() on array search →
Reply
#2

as it turned out, in the file \system\Model.php, line 442

$ this-> tempReturnType = $ this-> returnType; costs AFTER $ row = $ row-> getResult ($ this-> tempReturnType); I moved it before this expression and it worked.
Reply
#3

Change your model like this:

Code:
class UserModel extends Model
{
    protected $table      = 'users';
    protected $primaryKey = 'id_user';

    /*
    * Get all users
    */
    function get_all_users()
    {
        return $this->table('users')->orderBy('id_user', 'asc');
    }
}
Reply
#4

Your re-initializing the db and builder in your models constructor.

When you extend the Model you have access to $this->db and $this->builder.

You need to read up on CodeIgniter Models in the Users Guide.
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 02-18-2021, 08:03 PM by wdeda.)

Simple things work better.
Less is more.
Of course, simple sugestions.
Model:
PHP Code:
<?php<?php
namespace App\Models;

use 
CodeIgniter\Model;

class 
UserModel extends Model
{
    protected $table  'users';
    protected $primaryKey 'id';
    
    
public function numUsers()
    {
    $db $this->db;
    $count $db->table('users');
    return $count->countAllResults();
    }
    


Controller:
PHP Code:
<?php namespace App\Controllers;
    
    
use CodeIgniter\Controller;
    use App\Models\UserModel;

    class Users extends Controller {

    public function index() {
    
    $model 
= new UserModel();
    
    $data 
= [    
    
'users' =>$model->select('id_user, name_user')
    ->orderBy('id_user''asc')
    ->pagination(10),
    'pager' => $model->pager,
    'total' => $model->numUsers;  
    
];
    
    
// Standardization I use on the website
    $header = [
    'glbstrap'  => 'bootstrap'// general css
    'css'       => 'namelists'// specific css for certain pages
    'title'     => 'Simple example of pagination - dimonisse' // page title
    ];
    
    
echo view('templates/header'$header);
    echo view('users/user'$data);
    echo view('templates/footer');    
        
    
}



A portion of the View(pager)
PHP Code:
<div class="users-pages-index">
  <?php if ($total 10) { ?>
  <?php if ($pager) : ?>
  <?php $pagi_path '/user/namelists/users' ?>
  <?php $pager->setPath($pagi_path); ?>
  <?= $pager->links() ?>
  <?php endif ?>
  <?php ?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB