Home > Backend Development > PHP Tutorial > Detailed explanation of how to use order() in ThinkPHP, thinkphporder_PHP tutorial

Detailed explanation of how to use order() in ThinkPHP, thinkphporder_PHP tutorial

WBOY
Release: 2016-07-12 08:53:46
Original
1077 people have browsed it

Detailed explanation of how to use order() in ThinkPHP, thinkphporder

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();
Copy after login

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();
Copy after login

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();
Copy after login

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122897.htmlTechArticleDetailed explanation of the use of order() in ThinkPHP, thinkphporder 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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template