Welcome Guest, Not a member yet? Register   Sign In
Pagination with Custom model
#1

Hi all,
I currently use pagination with Codeigniter Model but I cannot find how to use it with CUSTOM models.
Here is my code:

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

use 
App\Models\CustomModel;

class 
Annunci extends BaseController{

  public function index() {
     
     $db 
db_connect();
     $model = new CustomModel($db);
     $annunci $model->annunci();

     return view('cet',$annunci);

  }
}

?>

And here is the MODEL

PHP Code:
public function annunci(){
  return $this->db->table('annunci')
 ->
orderBy('data''DESC')
 ->
get()
 ->
getResultArray();




I know I need to load the pager somewhere and somehow but cannot figure out how to do it. Can anyone please show me on the above code? Thanks a lot
Reply
#2

Pagination
What did you Try? What did you Get? What did you Expect?

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

(12-31-2020, 12:27 PM)InsiteFX Wrote: Pagination

I referred to documentation but there are no examples as to how to use it with custom models. I tried several times to add pieces of code on the one I showed but with no success. That's why I'am asking if anyone can show how to properly load the library and use pagination in the query since I was not able to.
Reply
#4

(This post was last modified: 01-04-2021, 04:03 AM by seunex.)

Pagination in the docs only talk about the model method only does not give hint for those using query builder. You see I also find it hard finding my way in this pagination too using query builder i could not get it so I end up using third party pagination library from github.
That solve my problems.
Reply
#5

What do you mean by custom model?

I have examples for pagination in these 2 articles:
https://includebeer.com/en/blog/how-to-b...r-4-part-4
https://includebeer.com/en/blog/how-to-b...r-4-part-5
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#6

This is how I am doing and working perfectly 

Code:
public function index($keyword = null)
    {
        $keyword = trim($this->request->getVar('keyword'));
        $Product = new Product();
        $data['title'] = 'All Products';
        $data['products'] = $Product->like('name',$keyword)
                            ->orderBy('created_at','DESC')
                            ->paginate(15);
        $data['pager'] = $Product->pager;
        $data['currentPage'] = $Product->pager->getCurrentPage('default'); // The current page number
        $data['totalPages']  = $Product->pager->getPageCount('default');  // The total page count
        return view('Travo\Admin\Views\Products\Index', $data);
    }
Reply
#7

(This post was last modified: 01-04-2021, 01:56 PM by Mostafa Khudair.)

(12-31-2020, 03:59 AM)Here we go. Wrote: CONTROLLER:
PHP Code:
<?php

namespace App\Controllers;

use 
App\Models\CustomModel;

class 
Annunci extends BaseController
{

  public function index()
  {
     $model = new CustomModel();
     $data['annunci'] = $model->annunci();
     
$data['page']    = $model->pager;

     return view('cet'$data);
  }


MODEL:

PHP Code:
protected $table 'annunci';

public function 
annunci()
{
  return $this->orderBy('data''DESC')->paginate();


VIEW:
PHP Code:
<?php foreach ($annunci as $value): ?>
  <span><?= $value->id ?></span>
<?php endforeach ?>

It's very recommended to read userguide
Reply
#8

(This post was last modified: 01-07-2021, 02:34 AM by AndreaL.)

(01-04-2021, 05:33 AM)includebeer Wrote: What do you mean by custom model?

I have examples for pagination in these 2 articles:
https://includebeer.com/en/blog/how-to-b...r-4-part-4
https://includebeer.com/en/blog/how-to-b...r-4-part-5

Hi, if you refer to my question I have posted the code.
By "custom model" I mean that I am not extending codeigniter's model but I have built a model of my own.
If I extend Codeigniter's model I have no problem with pagination. But with the custom model it does not work.
On the code I have posted, how would you add pagination?

(01-04-2021, 10:04 AM)Mostafa Khudair Wrote:
(12-31-2020, 03:59 AM)Here we go. Wrote: CONTROLLER:
PHP Code:
<?php

namespace App\Controllers;

use 
App\Models\CustomModel;

class 
Annunci extends BaseController
{

  public function index()
  {
     $model = new CustomModel();
     $data['annunci'] = $model->annunci();
     $data['page']    $model->pager;

     return view('cet'$data);
  }


MODEL:

PHP Code:
protected $table 'annunci';

public function 
annunci()
{
  return $this->orderBy('data''DESC')->paginate();


VIEW:
PHP Code:
<?php foreach ($annunci as $value): ?>
  <span><?= $value->id ?></span>
<?php endforeach ?>

It's very recommended to read userguide

Hi,
I believe your answer assumes I am extending Codeigniter's model, am I right?
If so, I am not doing that. Mine is a custom model
Reply
#9

(This post was last modified: 01-07-2021, 04:23 AM by berendbotje91.)

(01-07-2021, 02:31 AM)AndreaL Wrote: Hi, if you refer to my question I have posted the code.
By "custom model" I mean that I am not extending codeigniter's model but I have built a model of my own.
If I extend Codeigniter's model I have no problem with pagination. But with the custom model it does not work.
On the code I have posted, how would you add pagination?
Your code doesn't really explain how it is considered a "custom" model. Its only a function. It also doesn't explain why you don't want to extend codeigniters model. You aren't doing anything different that would not be possible in an extended model. So to most reading this topic, the way to make pagination work for you is to extend CodeIgniters Model.

Because pagination is part of CodeIgniters Model, making a "custom model" also requires you to write your own code for pagination.
Reply
#10

Ok, it wasn’t clear your model do not extend CodeIgniter’s model. We just see one function.

If you really don’t want to extend the Model class you can take a look at how the pagination is implemented in this class: https://github.com/codeigniter4/CodeIgni....php#L1165

You will need to use the Pager service. You can’t call findAll() because it’s part of the Model class, so you will need to call the limit() function of the query builder to set the limit and offset for the select query.

PHP Code:
    /**
     * Works with $this->builder to get the Compiled Select to operate on.
     * Expects a GET variable (?page=2) that specifies the page of results
     * to display.
     *
     * @param integer $perPage
     * @param string  $group   Will be used by the pagination library
     *                         to identify a unique pagination set.
     * @param integer $page    Optional page number (useful when the page number is provided in different way)
     * @param integer $segment Optional URI segment number (if page number is provided by URI segment)
     *
     * @return array|null
     */
    
public function paginate(int $perPage nullstring $group 'default'int $page nullint $segment 0)
    {
        
$pager = \Config\Services::pager(nullnullfalse);

        if (
$segment)
        {
            
$pager->setSegment($segment);
        }

        
$page $page >= $page $pager->getCurrentPage($group);

        
$total $this->countAllResults(false);

        
// Store it in the Pager library so it can be
        // paginated in the views.
        
$this->pager $pager->store($group$page$perPage$total$segment);
        
$perPage     $this->pager->getPerPage($group);
        
$offset      = ($page 1) * $perPage;

        return 
$this->findAll($perPage$offset);
    } 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB