Pagination with Custom model |
Hi all,
I currently use pagination with Codeigniter Model but I cannot find how to use it with CUSTOM models. Here is my code: CONTROLLER PHP Code: <?php namespace App\Controllers; And here is the MODEL PHP Code: public function annunci(){ I know I need to load the pager somewhere and somehow but cannot figure out how to do it. Can anyone please show me on the above code? Thanks a lot
Pagination
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(12-31-2020, 12:27 PM)InsiteFX Wrote: Pagination I referred to documentation but there are no examples as to how to use it with custom models. I tried several times to add pieces of code on the one I showed but with no success. That's why I'am asking if anyone can show how to properly load the library and use pagination in the query since I was not able to.
Pagination in the docs only talk about the model method only does not give hint for those using query builder. You see I also find it hard finding my way in this pagination too using query builder i could not get it so I end up using third party pagination library from github.
That solve my problems.
What do you mean by custom model?
I have examples for pagination in these 2 articles: https://includebeer.com/en/blog/how-to-b...r-4-part-4 https://includebeer.com/en/blog/how-to-b...r-4-part-5
This is how I am doing and working perfectly
Code: public function index($keyword = null)
(12-31-2020, 03:59 AM)Here we go. Wrote: CONTROLLER: It's very recommended to read userguide (01-04-2021, 05:33 AM)includebeer Wrote: What do you mean by custom model? Hi, if you refer to my question I have posted the code. By "custom model" I mean that I am not extending codeigniter's model but I have built a model of my own. If I extend Codeigniter's model I have no problem with pagination. But with the custom model it does not work. On the code I have posted, how would you add pagination? (01-04-2021, 10:04 AM)Mostafa Khudair Wrote:(12-31-2020, 03:59 AM)Here we go. Wrote: CONTROLLER: Hi, I believe your answer assumes I am extending Codeigniter's model, am I right? If so, I am not doing that. Mine is a custom model
(01-07-2021, 02:31 AM)AndreaL Wrote: Hi, if you refer to my question I have posted the code.Your code doesn't really explain how it is considered a "custom" model. Its only a function. It also doesn't explain why you don't want to extend codeigniters model. You aren't doing anything different that would not be possible in an extended model. So to most reading this topic, the way to make pagination work for you is to extend CodeIgniters Model. Because pagination is part of CodeIgniters Model, making a "custom model" also requires you to write your own code for pagination.
Ok, it wasn’t clear your model do not extend CodeIgniter’s model. We just see one function.
If you really don’t want to extend the Model class you can take a look at how the pagination is implemented in this class: https://github.com/codeigniter4/CodeIgni....php#L1165 You will need to use the Pager service. You can’t call findAll() because it’s part of the Model class, so you will need to call the limit() function of the query builder to set the limit and offset for the select query. PHP Code: /** |
Welcome Guest, Not a member yet? Register Sign In |