![]() |
Using Pagination Library - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Using Pagination Library (/showthread.php?tid=80924) |
Using Pagination Library - cifan - 01-03-2022 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 RE: Using Pagination Library - captain-sensible - 01-04-2022 I just use pagination via model ; what i can't see in yours anywhere is including Code: ->paginate(10); RE: Using Pagination Library - cifan - 01-04-2022 (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? RE: Using Pagination Library - iRedds - 01-04-2022 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? RE: Using Pagination Library - captain-sensible - 01-05-2022 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 RE: Using Pagination Library - BilltheCat - 01-05-2022 Here's a simple example controller I use for paging through my logs table. Hopefully, it will help you get started. PHP Code: <?php |