[PHP] tp_搜索+连贯操作 →→→→→进入此内容的聊天室

来自 , 2019-12-05, 写在 PHP, 查看 105 次.
URL http://www.code666.cn/view/16fc18d7
  1. //编写search方法,实现搜索
  2.         public function search(){
  3.             //获取post的数据,根据数据组装查询的条件,根据条件从数据库中获取数据,返回给页面中遍历
  4.             if(isset($_POST['username']) && $_POST['username']!=null){
  5.                 $where['username']=array('like',"%{$_POST['username']}%");
  6.             }
  7.             if(isset($_POST['sex']) && $_POST['sex']!=null){
  8.                 $where['sex']=array('eq',$_POST['sex']);
  9.             }
  10.             $m=M('User');
  11.             $arr=$m->where($where)->select();
  12.             $this->assign('data',$arr);
  13.             $this->display('index');
  14.         }
  15.  
  16. # ThinkPHP 3.1.2 连贯操作
  17.  
  18. 本节课大纲:
  19. 一、常用连贯操作 (重点)
  20. 二、补充 (了解)
  21.  
  22.  
  23. ====================================================
  24. 一、常用连贯操作
  25. 1.where
  26. 帮助我们设置查询条件
  27. 2.order
  28. 对结果进行排序
  29. $arr=$m->order('id desc')->select();
  30. $arr=$m->order(array('id'=>'desc','sex'=>'asc'))->select();
  31. 3.limit
  32. 限制结果
  33. limit(2,5)
  34. limit('2,5')
  35. limit(10)//limit(0,10)
  36. 4.field
  37. 设置查询字段
  38. field('username as name,id')
  39. field(array('username'=>'name','id')
  40. field('id',true) //获取除了id以外的所有字段
  41. 5.table
  42. 6.group
  43. 7.having
  44. 二、补充
  45. alias 用于给当前数据表定义别名 字符串
  46. page 用于查询分页(内部会转换成limit) 字符串和数字
  47. join* 用于对查询的join支持 字符串和数组
  48. union* 用于对查询的union支持 字符串、数组和对象
  49. distinct 用于查询的distinct支持 布尔值
  50. lock 用于数据库的锁机制 布尔值
  51. cache 用于查询缓存 支持多个参数(以后在缓存部分再详细描述)
  52. relation 用于关联查询(需要关联模型扩展支持) 字符串
  53. validate 用于数据自动验证 数组
  54. auto 用于数据自动完成 数组
  55. filter 用于数据过滤 字符串
  56. scope* 用于命名范围 字符串、数组

回复 "tp_搜索+连贯操作"

这儿你可以回复上面这条便签

captcha