Using the built-in pagination functions |
Hi,
I would like to implement the pagination library for retrieving logs from the database. This is one of my methods in a model: PHP Code: public function getAllLogs(?string $searchString = null, int $limit = 50): array Please note that I have edited it a bit to be readable and safe here.... The manual tells me to do this: PHP Code: $model = new \App\Models\UserModel(); And how can I pass my variable $searchString ?
The Model::paginate() method uses the Model and QueryBuilder methods to retrieve data.
PHP Code: $model = new \App\Models\UserModel(); PHP Code: $model->where('a', 'b')->paginate(10); You can move the conditions to a separate method. PHP Code: //model Using $this->db->query() immediately executes the query and cannot be used with the Model::paginate() method.
To add to the response, if you want to use your method, you'll have to rely on manual pagination on the view. https://codeigniter.com/user_guide/libra...pagination
Your current query also does not support pagination as it lacks a variable offset (currently set to 0).
OK, I will use the query builder intead.
But that raises a new question.... In my controller I now have this: PHP Code: //used session to remember the searched value when browsing to another page in paginator My Logging Model now only has a createLogging() and deleteLogging() method. These are easily converted to the query builder and put into the controller, but should I? And what are the pro's of using the query builder over $this->db.->query?
Yes, I have read that.
But that's not really my question The paginator now works. But that raised a new question. Since the paginator is also kind of a query.... Which queries should I place in the controller, and which ones in the Model?
All queries should be placed in your model.
My Pagination uses my Bootstrap 5 Pagination Template. Model: PHP Code: /** Controller: PHP Code: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Just a following question....
In my view I want to show how many results are displayed. For example: Users: 20 of 35 I know there is a $pager->getTotal(). But is there also a method for returning the number of results on that page? |
Welcome Guest, Not a member yet? Register Sign In |