Welcome Guest, Not a member yet? Register   Sign In
How to make pagination for search results
#1

Hi!
I have the following model:

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;


class 
SearchModel extends MyModel
{

    protected $table 'webpage';
    public function get_results($search)
    {
        $db = \Config\Database::connect();
        $results;
        $query $this->db->query("SELECT webpageID, webpageTitle, 'webpage' AS recordType
                                        FROM webpage 
                                        WHERE webpageTitle LIKE '%"
.$this->db->escapeLikeString($search)."%' 
                                        AND deletedAt IS NULL
                                                
                                        UNION
                                        
                                        SELECT webpagecategoryID, webpagecategoryName, 'category' AS recordType
                                        FROM webpagecategory  
                                        WHERE  webpagecategoryName LIKE '%"
.$this->db->escapeLikeString($search)."%' 
                                        AND deletedAt IS NULL"
        );

      $results $query->getResult();    
      
return $results;
      
    
}

// End of file MyModel.php 

In my controller I have the following (related) code:
PHP Code:
$results $this->searchModel->get_results($search);
               
$pager 
= \Config\Services::pager();
$data = [
'webpages' => $results,
'pager' => $pager,
]; 

And in my view, after displaying results I have:
PHP Code:
<?= $pager->links(); ?>

Which does not work, I get just nice 1, which is not even a link.
Any help will be deeply apriciated.

Regards, Z
Reply
#2

Below, a basic and small example of pagination, not considering the arguments and type of query used.
Model:
PHP Code:
<?php
namespace App\Models;

use 
CodeIgniter\Model;

class 
TesteModel extends Model
{
    protected $table  'names';
    protected $primaryKey 'id';
    protected $returnType 'object';


    public function getNames($id)
    {
        $db $this->db;
        $query $db->table('names')
            ->where('id'$id)
            ->get();

        return $query->getResult();
    }

    public function numNames($id)
    {
        $db $this->db;        
        $count 
$db->table('names');
        $count->where('prof2 > 0');

        return $count->countAllResults();
    }


Controller:
PHP Code:
<?php

namespace App\Controllers;

// 20210207

use App\Models\TesteModel;
use 
CodeIgniter\Controller;

class 
Teste extends Controller
{

    public function names($id)
    {

        $testeModel = new TesteModel();

        $data = [
            'player'  => $testeModel->where('prof2 > 0')
                ->orderBy('name''asc')
                ->paginate(20),
            'pager'  => $testeModel->pager,
            'total'  => $testeModel->numMames($id)
        ];

        $header = [
            'css'  => 'teste',
            'title' => 'Teste - wdeda '
        ];

        echo view('templates/header'$header);
        echo view('teste'$data);
        echo view('templates/footer');

}


Reply
#3

Hello.
Thanks for posting your answer. However, it didn't help me. Even that I have added :
PHP Code:
protected $returnType 'object'

in my model and the :
PHP Code:
$data = [
                'webpages' => $results,
                // 'search' => $search,
                'pager' => $this->searchModel->pager,
                'total' => $this->searchModel->get_results($search),
                // 'categories' => $categories,
              
            
]; 

in my controller, nothing has changed, I still get the static 1, which is not even a link.
Anyone?
Reply
#4

You need to read the CodeIgniter 4 User Guide on Models.

Using CodeIgniter’s Model

Specifically on Configuring Your Model.
What did you Try? What did you Get? What did you Expect?

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

Hello.
I just read the Model documentation (I have read it before also), and I read the pointed part (model configuration) twice. However ,expect of the $returnType property, I could not find anything related to the pagination, hence I have no idea what should I change in my model to make the pagination to work. Can you be more specific please?
Regards, Z
Reply
#6

(This post was last modified: 09-30-2021, 02:16 AM by InsiteFX.)

The only thing I see you missing is the view pagination links.
PHP Code:
<!-- Pagination -->
<
div class="pagination justify-content-center my-4">
    <?php if ( ! empty($pager)) :
        echo $pager->links();
    endif ?>
</div> 
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 09-30-2021, 03:28 AM by t_5810.)

Hello

If you see my first post, you would see that I have in my view the 
PHP Code:
<?= $pager->links(); ?>

Never the less, I add your code, however, result is the same, I get 1 as link, but I see all 14 results on the page, not 10.
And page with which i test, displays 14 results and in config/Pager, $perPage is set to 10


Regards, Z 

Update:
After changing my controller code to: 
PHP Code:
$searchModel = new SearchModel();
$results $searchModel->get_results($search)->paginate(10);                  
            
            
$data 
= [
  'webpages' =>$results,
  'pager' => $sarchModel->pager,                
]; 

Now, i get the following error: Call to a member function paginate() on array
which points to this line:
PHP Code:
$results $searchModel->get_results($search)->paginate(10); 

I hope that this helps....
Reply
#8

Well your not really showing all the code here is a link on how to do Pagination in CodeIgniter 4.

CodeIgniter 4 Pagination with Search Filter Tutorial
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 09-30-2021, 06:07 AM by wdeda.)

I think I didn't know how to explain myself clearly, I'll try one more time:
In my example of Model there is a function that is not used, getName, this was intentional, to indicate that the Model can be just a reference of the table to be used by the paging, besides other fields relevant to the Model. In my case I just preferred to use the return as an object. In other words, you can assemble the search arguments in the Controller, observing, of course, the indication of the Model so that there is the identification of the table to be used.

I believe the problem you're currently having is because you're trying to define what should be paged from, I don't know if it's the correct term, of a data packet resulting from a search. It's like paging has a built-in foreach.

I am also assuming that $search has its origin in a search form, so it is necessary that the origin be given. Below is an example of what I use, and there may, of course, be more elaborate examples:
PHP Code:
$request service('request');
$terms $request->getVar('search'); 
search is the field defined in the form for the search argument that will be represented by the $terms variable.

'total' used in my example was just to demonstrate that you can add whatever you want to the $data array, to be sent to the View. In this case "total" is the number of items to be paged. In the view, in the header, I can use, for example:
PHP Code:
echo "20 items of $total"

By the way: it's created_at, updated_at and deleted_at, unless you've created something for a similar purpose.
Your query:
AND deletedAt IS NULL"

Allow me to suggest reading the pagination topic in the system manual:
https://codeigniter.com/user_guide/libra...ation.html
Reply
#10

You're passing a new instance of the $pager in. This one isn't attached to any results so doesn't know what to display, so it only displays the 1 you're seeing. Instead of passing the new instance in, you need to grab the one from the model.

PHP Code:
$results $this->searchModel->get_results($search);
               
$data = [
'webpages' => $results,
'pager' => $this->searchModel->pager,
]; 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB