How to make pagination for search results |
Hi!
I have the following model: PHP Code: namespace App\Models; In my controller I have the following (related) code: PHP Code: $results = $this->searchModel->get_results($search); And in my view, after displaying results I have: PHP Code: <?= $pager->links(); ?> Which does not work, I get just nice 1, which is not even a link. Any help will be deeply apriciated. Regards, Z
Below, a basic and small example of pagination, not considering the arguments and type of query used.
Model: PHP Code: <?php Controller: PHP Code: <?php
Hello.
Thanks for posting your answer. However, it didn't help me. Even that I have added : PHP Code: protected $returnType = 'object'; in my model and the : PHP Code: $data = [ in my controller, nothing has changed, I still get the static 1, which is not even a link. Anyone?
You need to read the CodeIgniter 4 User Guide on Models.
Using CodeIgniter’s Model Specifically on Configuring Your Model. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Hello.
I just read the Model documentation (I have read it before also), and I read the pointed part (model configuration) twice. However ,expect of the $returnType property, I could not find anything related to the pagination, hence I have no idea what should I change in my model to make the pagination to work. Can you be more specific please? Regards, Z
The only thing I see you missing is the view pagination links.
PHP Code: <!-- Pagination --> What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Hello
If you see my first post, you would see that I have in my view the PHP Code: <?= $pager->links(); ?> Update: After changing my controller code to: PHP Code: $searchModel = new SearchModel(); Now, i get the following error: Call to a member function paginate() on array which points to this line: PHP Code: $results = $searchModel->get_results($search)->paginate(10); I hope that this helps....
Well your not really showing all the code here is a link on how to do Pagination in CodeIgniter 4.
CodeIgniter 4 Pagination with Search Filter Tutorial What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I think I didn't know how to explain myself clearly, I'll try one more time:
In my example of Model there is a function that is not used, getName, this was intentional, to indicate that the Model can be just a reference of the table to be used by the paging, besides other fields relevant to the Model. In my case I just preferred to use the return as an object. In other words, you can assemble the search arguments in the Controller, observing, of course, the indication of the Model so that there is the identification of the table to be used. I believe the problem you're currently having is because you're trying to define what should be paged from, I don't know if it's the correct term, of a data packet resulting from a search. It's like paging has a built-in foreach. I am also assuming that $search has its origin in a search form, so it is necessary that the origin be given. Below is an example of what I use, and there may, of course, be more elaborate examples: PHP Code: $request = service('request'); 'total' used in my example was just to demonstrate that you can add whatever you want to the $data array, to be sent to the View. In this case "total" is the number of items to be paged. In the view, in the header, I can use, for example: PHP Code: echo "20 items of $total"; By the way: it's created_at, updated_at and deleted_at, unless you've created something for a similar purpose. Your query: AND deletedAt IS NULL" Allow me to suggest reading the pagination topic in the system manual: https://codeigniter.com/user_guide/libra...ation.html
You're passing a new instance of the $pager in. This one isn't attached to any results so doesn't know what to display, so it only displays the 1 you're seeing. Instead of passing the new instance in, you need to grab the one from the model.
PHP Code: $results = $this->searchModel->get_results($search); |
Welcome Guest, Not a member yet? Register Sign In |