thinkphp and php some query tips to share

Source: Internet
Author: User
This article is mainly to share with you thinkphp and PHP some small query tips, hope to help everyone.

1: Association query with the same field name query all fields

$goodsNum = $cartlistM->field (' *,a.id as CID ')->join (' Goods as B on a.goods_id = b.ID ')->where (Array (' a.ID ' => ; $_post[' Cartid '))->find ();

2: Query for a statement that places duplicate values by criteria

M (' Propertytake ')->group (' Batchnum ')->where ($w)->select ();

3: Remove the non-conforming array in the query Selsect () in the loop

foreach ($propertyArr as $key + $val) {$w [' mid '] = $this->mid; $w [' property_id '] = $val [' id '];//The query period has been 9 years $hasGet Year = count (M (' Propertytake ')->group (' Batchnum ')->where ($w)->select ());//Query the current asset's consumed resource quantity that has been harvested in this period $w [' Batchnum '] = $setBatch; $temp _num = M (' Propertytake ')->where ($w)->sum (' nums '); if (Empty ($temp _num)) {$temp _num = 0;} if ($hasGetYear >= $goodsArr [' Getmaxnum ']| | $temp _num>= $val [' Totaltree ']| | $val [' Totaltree ']<=0) {unset ($PROPERTYARR [$key]);} else {$treeNum + = $val [' Totaltree '];}}

4: Common functions

A) the Fmod () function returns the remainder of the floating-point number for division. b) unset ($saveData); destroy array C) strtotime (date (' Y '). ' -01-01 '); function d) ismobile (); Determine whether the current user is mobile e) Array_push ($arr, $val);//$arr The added array, $val the added value

5:thinkphp Transaction Processing Example://Turn on transaction local data

M ()->starttrans (); Submit M ()->commit (); Do not submit M ()->rollback ();

6:json parsing Json_decode (); JSON encryption Json_encode (value);

7: Call Parent Class (inherited Class)

Parent::memaddress ($list [' Province '], $list [' City '], $list [' District '];//parent:: Is the father, Memaddress is a function in the parent class

8: Query method of filtering query

Distanct (True) Example: $data =m (' user ')->distanct (True)->field (' score ')->order (' score ASC ')->select ();

9:thinkphp self-increment or decrement on field values

$newM->where (' id= ' $cid)->setinc (' Browser ', 1); Browser field value self-increment 1,setdec is self-reducing

10: Various loops (for Forech ....) Jump out of the loop return,break,continue three differences
Break is used to completely end a loop and jump out of the loop body. Regardless of the loop, once a break is encountered in the loop body, the system will completely end the loop and start executing the code after the loop. Break not only ends the loop in which it is located, but also ends its outer loop. At this point, you need to immediately follow a label, which identifies an outer loop. The tag in Java is an identifier followed by the English colon (:). And it must be placed before the loop statement.

public class breaktest2{public static void Main (string[] args) {//outer loop, outer as identifier outer:for (int i = 0; I &l T 5;        i++) {//inner loop for (int j = 0; J < 3; J + +) {System.out.println ("I's value is:" + i + "J is the value:" + j);           if (j = = 1) {//jump out of the loop identified by the outer tag.        Break outer; }      }    }  }}

The Continue function is somewhat similar to break, except that continue just aborts the loop and then starts the next loop. Break is a complete abort cycle.

public class continuetest{public static void Main (string[] args) {//A simple for loop for (int i = 0; i < 3; i++) {       System.out.println (the value of "I" is "+ i");       if (i = = 1) {//Ignore the remainder of the statement continue;    } System.out.println ("Output statement after continue"); }  }}

The return keyword is not designed to jump out of a loop, and the return function is to end a method. Once a return statement is executed in the loop body, the return statement ends the method, and the loop naturally ends with it. Unlike continue and break, return directly ends the entire method, regardless of how many layers of this return are within the loop.

public class returntest{public static void Main (string[] args) {//a simple for loop      for (int i = 0; i < 3; i++) {System.out.println (the value of "I is" + i);      if (i = = 1) {return;    } System.out.println ("Output statement after return"); }}} 

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.