Welcome Guest, Not a member yet? Register   Sign In
Raw Query Pagination
#1

Dear All,
how to create pagination in CI4 using raw query?

my model:
Code:
public function getData()
{
$query= $this->db->query('Select * from get_all_data');
return $query;
}
my controller :
Code:
$curentPage = 1;
$rowPerpage = 10;
$list_data= $this->CustomerModel->getData();
        $data = [
            'title' => 'List Data',
            'data' => $list_data->paginate($rowPerpage,'get_all_data'),
            'pager' => $this->CustomerModel->pager,
            'tableName' =>'get_all_data',
            'curentPage' => [
                'curentPage' => $curentPage,
                'rowPerpage' => $rowPerpage
            ]
more information:
 'get_all_data' is a mysql view that i create in database.

so i want show data in CI4 view with pagination.


best regards
Reply
#2

(This post was last modified: 10-30-2021, 02:46 AM by captain-sensible. Edit Reason: added something i forgot )

whats the advantage of that when you are using "*" which gets all.

in a model you define fields:

Code:
class BlogModel extends Model

{

protected $table      = 'blog';
protected $primaryKey = 'Id';
protected $allowedFields = ['title','article','image','slug','date'];
protected $limit;
protected $offset;
protected $Id;
protected $title;
protected $article;
protected $slug;


via a Controller you can get a handle to Model eg
Code:
$handle = new BlogModel();    
                     $data = [
                         'title'=>'paginate',
                        'blogs' => $handle->paginate(5),
                        'pager' => $handle->pager,
                        'date'=>$this->myDate
                    ];
in a view you can for instance

Code:
foreach($blogs as $blogy)
{



echo "<h11><a href = blogArticle/" . $blogy['slug'] .  "> ".  $blogy['slug']  ."         </a> </h11> <br> ";  



}

the main point being you can get access to all field in the table. So basically (i think)


'blogs' => $handle->paginate(5) is saying blogs is an object which contains data in all fields that were listed in my model i.e "*"


and that blogs is passed to view bundled in $data
Reply
#3

(10-30-2021, 02:45 AM)captain-sensible Wrote: whats the advantage of that when you are using "*" which gets all.

in a model you define fields:

Code:
class BlogModel extends Model

{

protected $table      = 'blog';
protected $primaryKey = 'Id';
protected $allowedFields = ['title','article','image','slug','date'];
protected $limit;
protected $offset;
protected $Id;
protected $title;
protected $article;
protected $slug;


via a Controller you can get a handle to Model eg
Code:
$handle = new BlogModel();
$data = [
'title'=>'paginate',
'blogs' => $handle->paginate(5),
'pager' => $handle->pager,
'date'=>$this->myDate
];
in a view you can for instance

Code:
foreach($blogs as $blogy)
{



echo "<h11><a href = blogArticle/" . $blogy['slug'] .  "> ".  $blogy['slug']  ."        </a> </h11> <br> "; 



}

the main point being you can get access to all field in the table. So basically (i think)


'blogs' => $handle->paginate(5) is saying  blogs is an object  which contains  data in all fields that were listed in my model i.e "*"


and that blogs is passed to view  bundled in $data

Thank you for reply.
My question how to paging from raw CI4 query result.
if using the model i have no problem with that. Already used that.
i want show all data from mysql view.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB