Welcome Guest, Not a member yet? Register   Sign In
Pagination using ajax in codeigniter 4
#1

Hello guys...am trying to use this pagination class developed by codexworld, to implement Ajax pagination in codeigniter 4. Unfortunately the class was written to be used with codeigniter 3. I keep on getting an error on line 99 where there is this.
PHP Code:
$CI =& get_instance(); 

How do i deal with this issue?
Reply
#2

It was written for CodeIgniter 3 not 4. CodeIgniter 4 pagination is different then CodeIgniter 3.

CodeIgniter 4 pagination library would need to be extended and modified to work with ajax.
What did you Try? What did you Get? What did you Expect?

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

Well then; i will use the default pagination which comes with codeigniter 4 but i really do need assistance as the tutorials am seeing online, pagination is created by accessing the model directly like so.

PHP Code:
$this->model->paginate(5
What if i have a custom query in StockModel.php

PHP Code:
public function stockphotopagination($where) {
 $builder $this->db->table("stock_photos")
  ->select('photo_name')
  ->where($where)
  ->get()
  ->getResultArray();


And in the StockController.php

PHP Code:
public function home() {
  $model = new StockModel();
  $where = [
   "deletion" "no",
   "display" => "yes"
  ];
  $query $model->stockphotopagination($where);
  $data['results'] = $query;
  echo view('views/home'$data);



And in home.php view:

PHP Code:
foreach($results as $row) {
  echo $row['
  photo_name'
]. '<br>';


How do i create pagination for this?
Reply
#4

CodeIgniter 4 - Tutorial
How to build a basic web application with CodeIgniter 4 (part 4) Pagination
What did you Try? What did you Get? What did you Expect?

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

I removed the code from model and did everything in the controller.
Code:
$model = new StockModel();

$query = $model
  ->select('photo_name')
  ->where([
   "deletion" = "no",
   "display" => "yes"
])
  ->paginate(10);

$data['results'] = $query;
$data['pager'] = $model->pager;

echo view('views/home', $data);
It is working. Now was wondering how to get the right total for all results because i need to show something like:- 10 results available.
Reply
#6

(This post was last modified: 06-08-2021, 01:10 AM by InsiteFX.)

You can do it, it has methods for getting that data see the below blog method $data
PHP Code:
/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 *
 */
 
public function posts()
 {
 
$pager Services::pager();

 
$posts      = new PostModel($this->request);
 
$categories = new CategoryModel();

 
$data = [
 
'posts'      => $posts->getLivePosts()->paginate(3'group1'),
 
'pager'      => $posts->pager,
 
'currentPage' => $posts->pager->getCurrentPage('group1'),
 
'totalPages'  => $posts->pager->getPageCount('group1'),
 
'categories'  => $categories->getTopCategories(),
 
'title'      => 'Blog Home',
 
'pageHeading' => 'Blog',
 
'subHeading'  => 'Home',
 
'typography'  => Services::typography(),
 ];

 
// Make all variables global to all views. (Empty PHP File)
 
echo view('Insitefx\Blog\Views\view_data'$data);
 echo 
view('Insitefx\Blog\Views\posts\index');
 } 
I use custom Bootstrap Pagers for this which you can find the code in the forums here.

Here is the partial view code I use layouts.

PHP Code:
<!-- Pagination -->
<
div class="pagination justify-content-center mb-4">
 <?
php if ( ! empty($pager)) :
    //echo $pager->simpleLinks('group1', 'bs_simple');
    echo $pager->links('group1''bs_full');
 endif 
?>

 <!-- Bootstrap 4.5.2 code to show page 1 of 4 total pages using a button. -->
 <div class="btn-group pagination justify-content-center mb-4" role="group" aria-label="pager counts">
 &nbsp;&nbsp;&nbsp;
 <button type="button" class="btn btn-light"><?= 'Page '.$currentPage.' of '.$totalPages?></button>
 </div>
</div> 
hope that helps.
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: 06-08-2021, 07:29 PM by coderscvoen. Edit Reason: adding additional information )

(06-08-2021, 01:05 AM)InsiteFX Wrote: You can do it, it has methods for getting that data see the below blog method $data
PHP Code:
/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 *
 */
 
public function posts()
 {
 
$pager Services::pager();

 
$posts      = new PostModel($this->request);
 
$categories = new CategoryModel();

 
$data = [
 
'posts'      => $posts->getLivePosts()->paginate(3'group1'),
 
'pager'      => $posts->pager,
 
'currentPage' => $posts->pager->getCurrentPage('group1'),
 
'totalPages'  => $posts->pager->getPageCount('group1'),
 
'categories'  => $categories->getTopCategories(),
 
'title'      => 'Blog Home',
 
'pageHeading' => 'Blog',
 
'subHeading'  => 'Home',
 
'typography'  => Services::typography(),
 ];

 
// Make all variables global to all views. (Empty PHP File)
 
echo view('Insitefx\Blog\Views\view_data'$data);
 echo 
view('Insitefx\Blog\Views\posts\index');
 } 
I use custom Bootstrap Pagers for this which you can find the code in the forums here.

Here is the partial view code I use layouts.

PHP Code:
<!-- Pagination -->
<
div class="pagination justify-content-center mb-4">
 <?
php if ( ! empty($pager)) :
    //echo $pager->simpleLinks('group1', 'bs_simple');
    echo $pager->links('group1''bs_full');
 endif 
?>

 <!-- Bootstrap 4.5.2 code to show page 1 of 4 total pages using a button. -->
 <div class="btn-group pagination justify-content-center mb-4" role="group" aria-label="pager counts">
 &nbsp;&nbsp;&nbsp;
 <button type="button" class="btn btn-light"><?= 'Page '.$currentPage.' of '.$totalPages?></button>
 </div>
</div> 
hope that helps.

Thanks for this; especially the getCurrentPage() and getPageCount(). I also looked up the Pager.php class and found the getTotal() which displays the total of all records. I noticed that when i click on paginate link for example, to go from page 1 to page 2, values which were on page 1 are lost. How do i maintain those values? I was thinking of using session or something like that.
Reply
#8

You can use sessions or static variables.
What did you Try? What did you Get? What did you Expect?

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

(06-08-2021, 08:53 PM)InsiteFX Wrote: You can use sessions or static variables.

Thanks for the assistance.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB