Using Pagination Library |
Hi all, I want to use the default pagination library coming with CI4 for paginating posts in a page. So, I created the following controller. The result of a query builder is stored in the protected variables $posts. In the index page I have inserted the corresponding code for links creation (<?= $pager->links() ?>). Regardless the number of posts in the page, the pager always creates one link (page). See image below. How I can tell to the pager to create more links (pages)? FYI. I have set $perPage public variable equal to 3 in app/Config/Pager.php.
PHP Code: <?php
(01-04-2022, 08:19 AM)captain-sensible Wrote: I just use pagination via model ; what i can't see in yours anywhere is including I'm aware of this. But the problem is that you can't paginate results of a query builder. If you do something like this: PHP Code: $query_posts = $db->query("SELECT * FROM posts ORDER BY created_at DESC")->paginate(3); the following error will be raised: Call to undefined method CodeIgniter\Database\SQLite3\Result::paginate(). I don't want to use the <post> model to retrieve records from database. In other words: Is it possible to paginate the results of a query builder?
You have only two ways to solve your problem.
1. Use pagination of the model. 2. Write your own implementation of pagination. I recommend the second way. Only then will you understand how pagination works. And then, looking at the original code, you will feel ashamed. A comment about your code. $db->query() returns the query result. Services::pager() returns an instance of the pagination class. But they are not related to each other. What kind of magic do you expect?
01-05-2022, 05:29 AM
(This post was last modified: 01-05-2022, 05:36 AM by captain-sensible. Edit Reason: just remembered something )
yeah you can write your own pagination code I did it on another framework called fat free . The basics are get results, do a count on results , split number of results into arbitrary amount ; i remember i used ceil() function of php so as to get whole numbers and modulo ; i borked by old external hard drive where the code was otherwise i would just paste it for you
I think i also used urls like domain.com/page/$number then using that variable $number to get the right segment of the posts and i think i also used the total amount of results, divided by arbitrary number to know the number of view link pages to generate . if i can remember more i will post it
01-05-2022, 12:41 PM
(This post was last modified: 01-05-2022, 12:44 PM by BilltheCat. Edit Reason: typo )
Here's a simple example controller I use for paging through my logs table. Hopefully, it will help you get started.
PHP Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |