I need next & previous id record in database on Yii framework to make navigation buttons next and back ?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I made a function to get those ids your looking for. I suggest you to declare it in the model:
So to use it all you have to do do is this:
It will return the corresponding id of the next or previous record.
I didn't test it, so give it a try and if something goes wrong please let me know.
Taking the original answer and adapting it for Yii2 with a little clean up:
I added following functions in my model in Yii2:
The previous solutions are problematic when you get the the first or last record and they are making multiple calls to the database. Here is my working solution which operates on one query, handles end-of-table and disables the buttons at end-of-table:
Within the model:
Within the controller:
Within the view:
Make a private var that is used to pass info to other functions.
In Model:
Check the code generated in the ViewDetal link and modify for the Prev/Net links in the _view file using
in the array('id'=>#) section.
My implementation is based on SearchModel.
Controller:
OrderSearch:
PS: be sure if you can use
array_column
for array of objects. This works good in PHP 7+ but in lower versions you got to extractid
by yourself. Maybe it's good idea to usearray_walk
orarray_filter
in PHP 5.4+