Home >Backend Development >PHP Tutorial >Detailed explanation of how to use order() in ThinkPHP, thinkphporder_PHP tutorial
This article introduces the use of order() method in ThinkPHP. The order method can be used to sort the results of database operations. That is equivalent to an order by clause in the select statement.
The order method is one of the coherent operation methods of the model and is used to sort the results of database operations. That is equivalent to an order by clause in the select statement.
Usage
$Model->where('status=1')->order('id desc')->limit(5)->select();
Note: There is no order in the continuous operation methods. You can change the calling order at will before calling the select method.
Supports sorting of multiple fields, for example:
$Model->where('status=1')->order('id desc,status')->limit(5)->select();
If desc or asc collation is not specified, the default is asc.
If your field conflicts with the mysql keyword, it is recommended to call it in array mode, for example:
$Model->where('status=1')->order(array('order','id'=>'desc'))->limit(5)->select();
Supplement:
Thinkphp cannot use ->Two solutions for order() sorting!
Using ThinkPHP, I found that I cannot use ->order($order) to sort.
$order = " info.date2 desc ";
Unfortunately, the result of writing order like this becomes order by date2 limit... desc is missing.
Solution 1:
There cannot be any spaces on both sides of $order, $order = "info.date2 desc"; (correct). $order = "info.date2 desc"; (Error!)
Solution 2:
Open the file: D:WebSiteZbphp.comwwwThinkPHPExtendModelViewModel.class.php
Modify line 136 to $array = explode(' ', trim($order)); add trim and save, as shown in the picture:
It is recommended to use the second method, but we hope that thinkphp official website can correct this small problem to prevent users from modifying the kernel code themselves
This is all about how to use order() in ThinkPHP. I hope it will be helpful to you. At the same time, I also thank you very much for your support of the Bangkejia website!