//组合查询
$d['name']=array("like", "%".$_GET['keyword']."%"); $d['tel']=array("like", "%".$_GET['keyword']."%"); $d['_logic'] = 'or'; $c['_complex'] = $d;
thinkphp 多对多
namespace HomeModel; use ThinkModelRelationModel; class UserModel extends RelationModel{ protected $_link=array( "groupx"=>array( "mapping_type"=>self::MANY_TO_MANY, "foreign_key"=>"gid",//中间表的字段 "relation_foreign_key"=>"uid",//中间表的字段 "relation_table"=>"user_group" ) ); }
数据库配置:
ThinkPHP/Conf/convention.php
1
2
3
4
|
{ backgroud: red ; } |
根据表单插入
1
2
3
4
5
6
7
8
9
10
11
12
|
$Form = D( 'think_form' ); if ( $Form ->create()) { $result = $Form ->add(); if ( $result ) { $this ->success( '数据添加成功!' ); } else { $this ->error( '数据添加错误!' ); } } else { $this ->error( $Form ->getError()); } |
自定义插入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$Form = D( 'think_form' ); $d [ 'title' ] = "已经更改了 " ; $d [ 'content' ] = "已经更改了content " ; $d [ 'create_time' ] = "23" ; if ( $Form ->create()) { $result = $Form ->add( $d ); if ( $result ) { $this ->success( '数据添加成功!' ); } else { $this ->error( '数据添加错误!' ); } } else { $this ->error( $Form ->getError()); } |
读数据
{$data.id}
1
2
3
4
5
6
7
8
9
10
11
|
public function read( $id =0){ $Form = M( 'Form' ); // 读取数据 $data = $Form ->find( $id ); if ( $data ) { $this ->assign( 'data' , $data ); // 模板变量赋值 } else { $this ->error( '数据错误' ); } $this ->display(); } |
修改数据(表单 )
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public function update(){ $Form = D( 'Form' ); if ( $Form ->create()) { $result = $Form ->save(); if ( $result ) { $this ->success( '操作成功!' ); } else { $this ->error( '写入错误!' ); } } else { $this ->error( $Form ->getError()); } } |
修改数据(自定义)
1
2
3
4
5
6
7
8
|
$Form = M( "Form" ); // 要修改的数据对象属性赋值 $data [ 'id' ] = 5; $data [ 'title' ] = 'ThinkPHP' ; $data [ 'content' ] = 'ThinkPHP3.2.3版本发布' ; $Form ->save( $data ); // 根据条件保存修改的数据 $Form ->where( 'id=5' )->save(); // 根据条件保存修改的数据 $Form ->where( 'id=5' )->setField( 'title' , 'ThinkPHP' ); |
自增自减
1
2
3
4
5
|
$User = M( "User" ); // 实例化User对象 $User ->where( 'id=5' )->setInc( 'score' ,3); // 用户的积分加3 $User ->where( 'id=5' )->setInc( 'score' ); // 用户的积分加1 $User ->where( 'id=5' )->setDec( 'score' ,5); // 用户的积分减5 $User ->where( 'id=5' )->setDec( 'score' ); // 用户的积分减1 |
删除 数据
1
2
3
4
5
|
$Form = M( 'Form' ); $Form -> delete (5); $User ->where( 'id=5' )-> delete (); // 删除id为5的用户数据 $User -> delete ( '1,2,5' ); // 删除主键为1,2和5的用户数据 $User ->where( 'status=0' )-> delete (); // 删除所有状态为0的用户数据 |
查询数据
1
2
3
|
$User = M( "User" ); // 实例化User对象 $condition [ 'name' ] = 'thinkphp' ; $condition [ 'account' ] = 'thinkphp' ; |
1
2
3
4
|
$condition [ 'status' ] = array ( 'neq' ,-1); $condition [ '_logic' ] = 'OR' ; // 把查询条件传入查询方法 $User ->where( $condition )->select(); |
连贯查询
1
|
$User ->where( 'status=1' )->order( 'create_time' )->limit(10)->select(); |
变量
1
2
|
echo I( 'get.id' ,0); // 如果不存在$_GET['id'] 则返回0 echo I( 'get.name' , '' ); // 如果不存在$_GET['name'] 则返回空字符串 |
模版
1
2
|
// 表示调用Member控制器下面的read模板 $this ->display( 'Member:read' ); |
VOLIST
1
2
3
4
5
6
7
8
9
10
11
|
$User = M( 'User' ); $list = $User ->limit(10)->select(); $this ->assign( 'list' , $list ); <volist name= "list" id= "vo" > { $vo .id}:{ $vo .name}<br/> </volist> <volist name= "list" id= "vo" offset= "5" length= '10' > { $vo .name} </volist> |
FOREACH
1
2
3
|
< foreach name= "list" item= "vo" > { $vo .id}:{ $vo .name} </ foreach > |
比较
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<eq name= "name" value= "value" >value</eq> <eq name= "name" value= "value" > 相等 < else /> 不相等 </eq> 大于 <gt name= "name" value= "5" >value</gt> 小于 <egt name= "name" value= "5" >value</egt> <eq name= "vo.name" value= "5" > { $vo .name} </eq> 字符串长度==5 <eq name= "vo:name|strlen" value= "5" >{ $vo .name}</eq> <in name= "id" value= "1,2,3" > id在范围内 </in> <notin name= "id" value= "1,2,3" > id不在范围内 </notin> <notbetween name= "id" value= "1,10" > 输出内容2 </notbetween> <between name= "id" value= "1,10" > 输出内容1 < else /> 输出内容2 </between> <between name= "id" value= "A,Z" > 输出内容1 </between> <present name= "name" > name已经赋值 < else /> name还没有赋值 </present> < empty name= "Think.get.name" > $_GET [ 'name' ]为空值 </ empty > <defined name= "NAME" > NAME常量已经定义 < else /> NAME常量未定义 </defined> <php> echo 'Hello,world!' ;</php>
|