• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search and Pagination

#1
Pagination works simple and well when I list my db with 600 records

But when I have to include a search, only the first page of the pagination works well and counts the records correctly, when I click on the second page the system disregards the item searched and returns to list all the pages.

Please help me to implement the code correctly.

Code:
public function search(){     
            
        $model = new MyModel;
        $name = $this->request->getVar('name') ;
    
        $data = [
            'value' => $model->orderBy('uf', 'asc')
            ->like('name', $name)
            ->paginate(10),
                    
            'pager' => $model->pager
            ];


    echo view('mycontroller/search',$data);

}
Reply

#2
(04-20-2020, 11:40 AM)Maria Renata Wrote: Pagination works simple and well when I list my db with 600 records

But when I have to include a search, only the first page of the pagination works well and counts the records correctly, when I click on the second page the system disregards the item searched and returns to list all the pages.

Please help me to implement the code correctly.

Code:
public function search(){     

$model = new MyModel;
$name = $this->request->getVar('name') ;

$data = [
'value' => $model->orderBy('uf', 'asc')
->like('name', $name)
->paginate(10),

'pager' => $model->pager
];


echo view('mycontroller/search',$data);

}

Hi!
Try this.
In the example below you can bring all the results from the table or just one field.
If no field name is given, all the records in the table will be returned, otherwise, only the records for the given field, name, will be returned.

PHP Code:
-<?php

// 20200420

namespace App\Models;

use 
CodeIgniter\Model;

class 
MyModel extends Model
{
    protected $table  'users';
    
    
    
public function getNames($name null) {
        if ($name === null)
        {
                return $this->findAll();
        }

        return $this->asArray() // It can be object too.
                    ->like(['name' => $name]);
                    // ->first(); <= If you just want the first name found.
    }


Reply

#3
It only works for me when I enter a variable with the search term.
I tried to use Cookie or Session to store the value so it didn't work.
If I set the value inside the controller then search and paginate correctly.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.