How to Use order () in ThinkPHP _ php instance

Source: Internet
Author: User
This article describes the usage of the order () method of ThinkPHP. The order method can be used to sort database operation results. That is equivalent to an orderby clause in the select statement. This article describes the usage of the order () method of ThinkPHP. The order method can be used to sort database operation results. 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: coherent operation methods have no sequence. You can change the call sequence before calling the select method.
Supports sorting 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 value is asc.

If your field conflicts with the mysql keyword, we recommend that you use the array method, for example:

$Model->where('status=1')->order(array('order','id'=>'desc'))->limit(5)->select();

Supplement:

Thinkphp cannot use the-> order () sorting solution!

When ThinkPHP is used, you cannot use-> order ($ order) for sorting.

$ Order = "info. date2 desc ";

Unfortunately, the result order is changed to order by date2 limit... desc.

Solution 1:

$ Order cannot contain any space on both sides, $ 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 row 136th to $ array = explode ('', trim ($ order), and add trim to save ,:

The second method is recommended, but we expect thinkphp official website to fix this small problem, so that you do not need to modify the kernel code yourself.

This article introduces how to use order () in ThinkPHP. I hope it will help you and thank you very much for your support for the script home website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.