• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pagination

#1
How to use pagination through MySQL query instead of using just the Model name?
Controller code:
PHP Code:
namespace App\Controllers;
 
use 
CodeIgniter\Controller;
use 
App\Models\Model_pagination;
 
class 
Pagination extends Controller
{
    public function index()
    {    
        $model 
= new Model_pagination();
 
        $data = [
            'users' => $model->paginate(10),
            'pager' => $model->pager
        
];
        
        
return view('pagination'$data);
    
Model Code
PHP Code:
namespace App\Models;
use 
CodeIgniter\Database\ConnectionInterface;
use 
CodeIgniter\Model;
 
class 
Model_pagination extends Model
{
    protected $table 'users';
 
    protected $allowedFields = ['ID','unique_id_number''role'];


NOTE: Please help me out using MYSQL query with user defined JOINS or UNION etc instead of using just the table name in Model (Table: users here). I want to use userdefined queries instead of mere table name and allowed fields
Reply

#2
In your Controller
PHP Code:
$pager = \Config\Services::pager();
 
$model = new TestModel();
 
 
$this->data['root'] = [
            'data'  => $model->paginate(10),
            'pager' => $model->pager
        
]; 

In your Model, Overrite system function paginate
PHP Code:
public function paginate(int $perPage nullstring $group 'default'int $page nullint $segment 0)
 {
 
$this->table 'lott_index';
 
$pager = \Config\Services::pager(nullnullfalse);

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

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

 
// Get Total Records - Your query here to get Total Records - example below
 
$sql  "SELECT COUNT(id) AS total FROM lott_index LIMIT 1";
 
$query $this->db->query($sql);
 if (
count($query->getResult()) == 1) {
 
$row  $query->getRow();
 
$total intval($row->total);
 } else {
 
$total 0;
 }

 
// 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;

 
// Your query here to get Data For Per Page, example below
 
$query  " SELECT *";
 
$query .= " FROM lott_index";
 
$query .= " ORDER BY dates";
 
$query .= " LIMIT ".$offset.",".$perPage;
 
$query  $this->db->query($query);

 return 
$query;
 } 

This working fine for custom QUERY...

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#3
Thank you so much nc03061981. Overwriting the system method is such a cool idea. God Bless You and CI Team.... Love...
Reply

#4
I think CI4 have great Core System
User can overrite but core still working fine
Thanks CI4 Team

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#5
Yeah! Definitely... Thanks to CI 4 team...
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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