The order method in thinkphp3.2 is used to sort the results of the operation. It is one of the coherent operation methods of the model. It is equivalent to an order by clause in the select statement. The usage is "$Model->where ()->order()->limit()->select()".
The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.
The order method is one of the coherent operation methods of the model and is used to sort the results of the operation.
Usage is as follows:
$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 the desc or asc sorting rule 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();
Additional:
Thinkphp cannot be used ->order() Two solutions for 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:\WebSite\Zbphp.com\www\ThinkPHP\Extend\Model\ ViewModel.class.php
Modify line 136 to $array = explode(' ', trim($order)); add trim and save, as shown in the figure:
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to use the order method in thinkphp3.2. For more information, please follow other related articles on the PHP Chinese website!