Welcome Guest, Not a member yet? Register   Sign In
Using the built-in pagination functions
#1

(This post was last modified: 01-20-2022, 05:35 AM by sjender.)

Hi,
I would like to implement the pagination library for retrieving logs from the database.
This is one of my methods in a model:
PHP Code:
    public function getAllLogs(?string $searchString nullint $limit 50): array
    {
        $getLogs $this->db->query("SELECT * FROM `logs` WHERE `string` = ? ORDER BY `insert_date` DESC LIMIT 0, ?", [$searchString$limit]);

        return $getLogs->getResultArray()?? [];
    

Please note that I have edited it a bit to be readable and safe here....
The manual tells me to do this:
PHP Code:
        $model = new \App\Models\UserModel();

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

        echo view('users/index'$data);
    
But how does this pagination knows  which method it should call (in my case getAllLogs).
And how can I pass my variable $searchString ?
Reply
#2

The Model::paginate() method uses the Model and QueryBuilder methods to retrieve data.
PHP Code:
$model = new \App\Models\UserModel();
$model->paginate(10); // calls inside $model->findAll(10); 
To add selection criteria
PHP Code:
$model->where('a''b')->paginate(10); 

You can move the conditions to a separate method.
PHP Code:
//model
public function myConditions($a)
{
    
$this->builder()->where('a'$a);
    return 
$this;
}

$model->myConditions($a)->paginate(10); 

Using $this->db->query() immediately executes the query and cannot be used with the Model::paginate() method.
Reply
#3

To add to the response, if you want to use your method, you'll have to rely on manual pagination on the view. https://codeigniter.com/user_guide/libra...pagination

Your current query also does not support pagination as it lacks a variable offset (currently set to 0).
Reply
#4

OK, I will use the query builder intead.
But that raises a new question....
In my controller I now have this:
PHP Code:
//used session to remember the searched value when browsing to another page in paginator
$logs $loggingModel->like('request'$this->session->get('loggingSearch') ?? '')->orderBy('insert_date''DESC')->paginate(20); 
But if I can (and should????) do this is the controller, do I need any queries at all in the Model? Or do I only specify the used table, etc.?
My Logging Model now only has a createLogging() and deleteLogging() method. These are easily converted to the query builder and put into the controller, but should I?
And what are the pro's of using the query builder over $this->db.->query?
Reply
#5

See Important in https://codeigniter4.github.io/CodeIgnit...se-results
Reply
#6

Yes, I have read that.

But that's not really my question Smile

The paginator now works.
But that raised a new question.
Since the paginator is also kind of a query....
Which queries should I place in the controller, and which ones in the Model?
Reply
#7

All queries should be placed in your model.
My Pagination uses my Bootstrap 5 Pagination Template.

Model:
PHP Code:
/**
 * getLivePosts ()
 * -------------------------------------------------------------------
 *
 * @return PostModel
 */
 
public function getLivePosts() : PostModel
 
{
 
$builder $this->builder();

 
$builder->where('status''published')
 
        ->orderBy('created_at''desc');

        // for method chaining
 
return $this;
 } 

Controller:
PHP Code:
/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 * Get all Blog live posts
 * 
 */
public function posts()
{
    $pager Services::pager();

    $posts      = new PostModel();
    $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(),
    ];

    $data $this->mergeGlobalData($data);

    echo view('Insitefx\Blog\Views\posts\index_posts'$data);

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

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

Just a following question....

In my view I want to show how many results are displayed.
For example:

Users: 20 of 35

I know there is a $pager->getTotal().
But is there also a method for returning the number of results on that page?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB