thinkphp笔记
2016-05-09 10:44
471 查看
最近做的一个项目使用thinkphp3.2做的。这里总结一下 更新数据可以用$data[''],save之前一定要写where条件。不然是不能入库的 已一个数组作为条件的for循环查询数据库: $arr=('user_id'=>'1','user_id'=>'2','user_id'=>'3','user_id'=>'4','user_id'=>'5'); $array=aray(); for($arr as $key){ $test=M('test')->quey("select * from test where user_id={$key['user_id']}"); $array[]=$test;}//花括号可以让双引号里面的单引号转义html中可以用volist标签循环。,比如到我们从数据库查询出来的数据,就可以读取每一组数据中的$vo['name']这个属性Where 条件表达式格式为:
$map['字段名'] = array('表达式', '操作条件');其中 $map 是一个普通的数组变量,可以根据自己需求而命名。上述格式中的表达式实际是运算符的意义:ThinkPHP运算符 与 SQL运算符 对照表
TP运算符 | SQL运算符 | 例子 | 实际查询条件 |
---|---|---|---|
eq | = | $map['id'] = array('eq',100); | 等效于:$map['id'] = 100; |
neq | != | $map['id'] = array('neq',100); | id != 100 |
gt | > | $map['id'] = array('gt',100); | id > 100 |
egt | >= | $map['id'] = array('egt',100); | id >= 100 |
lt | < | $map['id'] = array('lt',100); | id < 100 |
elt | <= | $map['id'] = array('elt',100); | id <= 100 |
like | like | $map<'username'> = array('like','Admin%'); | username like 'Admin%' |
between | between and | $map['id'] = array('between','1,8'); | id BETWEEN 1 AND 8 |
not between | not between and | $map['id'] = array('not between','1,8'); | id NOT BETWEEN 1 AND 8 |
in | in | $map['id'] = array('in','1,5,8'); | id in(1,5,8) |
not in | not in | $map['id'] = array('not in','1,5,8'); | id not in(1,5,8) |
and(默认) | and | $map['id'] = array(array('gt',1),array('lt',10)); | (id > 1) AND (id < 10) |
or | or | $map['id'] = array(array('gt',3),array('lt',10), 'or'); | (id > 3) OR (id < 10) |
xor(异或) | xor | 两个输入中只有一个是true时,结果为true,否则为false,例子略。 | 1 xor 1 = 0 |
exp | 综合表达式 | $map['id'] = array('exp','in(1,3,8)'); | $map['id'] = array('in','1,3,8'); |
补充说明
同 SQL 一样,ThinkPHP运算符不区分大小写,eq 与 EQ 一样。between、 in 条件支持字符串或者数组,即下面两种写法是等效的:$map['id'] = array('not in','1,5,8'); $map['id'] = array('not in',array('1','5','8'));
exp 表达式
上表中的 exp 不是一个运算符,而是一个综合表达式以支持更复杂的条件设置。exp 的操作条件不会被当成字符串,可以使用任何 SQL 支持的语法,包括使用函数和字段名称。exp 不仅用于 where 条件,也可以用于数据更新,如:$Dao = M("Article"); // 构建 save 的数据数组,文章点击数+1 $data['id'] = 10; $data['counter'] = array('exp','counter+1'); // 根据条件保存修改的数据 $User->save($data); U方法 {:U('Home/Index/index')},跳转链接; {:U('Home/Admin/Delete',array('id'=>$ko.id.))} thinkphp中这样使用会报错,需要将数组中的表达格式改为array('id'=>$ko['id']) 隐藏域:比如我们在后台管理文章的时候,需要修改某个文章,这时候的id需要用到两次,有可能会导致id传一次查看内容,提交的时候读取不到id的值, 我们可以把这个id在点击修改文章的时候, 放到一个隐藏的input框的value中,把该input框隐藏,提交修改的时候,读取该隐藏框的value值 官网函数参考教程:http://www.thinkphp.cn/document/309.html 生成图片缩略图 $path = $info['photo']['rootPath'].'/'.$info['photo']['savepath'].$info['photo']['savename'];//$path等于我们上传的的原图片文件
$image = new \Think\Image(); $image->open('./'.$path); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $name = str_shufuller(time()); $image->thumb(150, 150)->save('./'.$info['photo']['rootPath'].'/'.$info['photo']['savepath'].$name.'.jpg'); 打开图路径和保存图片都需要加上./才行,否则会报错
thinkphp接受编辑器上传的内容,存入到数据库被转义成>,$bnsp这种。一般是I函数用php函数htmlspecialchars过滤了,在html页面输出的时候 htmlspecialchars_decode()函数转义一下就能输出
相关文章推荐
- php 其他常用文件处理函数
- PHP 中的文件处理技术
- php简单复制文件的方法
- php跨域
- yii学习笔记
- win10环境PHP 7 安装配置【教程】
- urldecode.php
- ubuntu php编译安装 openssl/evp.h: 没有那个文件或目录
- PHP培训实战教程之PHP函数
- PHP之误解
- PHP面向对象的进阶学习(抽像类、接口、final、类常量)
- urlencode.php
- php多文件上传
- sftp 限制用户登陆指定目录(家目录)
- PHP 使用 password_hash() 给密码加密
- Ubuntu PHP 环境配置
- php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
- php自动提交表单的方法(基于fsockopen与curl)
- SMTP客户端未通过身份验证等多种错误解决方案分享
- showdetail2.php