-
coderscvoen Junior Member
 
-
Posts: 16
Threads: 5
Joined: Mar 2021
Reputation:
0
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.
How do i deal with this issue?
-
coderscvoen Junior Member
 
-
Posts: 16
Threads: 5
Joined: Mar 2021
Reputation:
0
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?
-
InsiteFX Super Moderator
     
-
Posts: 6,729
Threads: 344
Joined: Oct 2014
Reputation:
246
-
coderscvoen Junior Member
 
-
Posts: 16
Threads: 5
Joined: Mar 2021
Reputation:
0
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.
-
InsiteFX Super Moderator
     
-
Posts: 6,729
Threads: 344
Joined: Oct 2014
Reputation:
246
06-08-2021, 01:05 AM
(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"> <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 )
-
coderscvoen Junior Member
 
-
Posts: 16
Threads: 5
Joined: Mar 2021
Reputation:
0
06-08-2021, 10:58 AM
(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"> <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.
|