Home > PHP Framework > ThinkPHP > body text

How to use the order method in thinkphp3.2

WBOY
Release: 2022-04-07 16:01:10
Original
2744 people have browsed it

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()".

How to use the order method in thinkphp3.2

The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.

How to use the order method in thinkphp3.2

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

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:

How to use the order method in thinkphp3.2

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!

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