原理:$map['r_name'] = [['like', '普通%'], ['like', '牛逼%'], ['like', '可爱%'], 'or'];
前端传递的参数:
location:重庆,广东省,北京,云浮,河南
tag:颜值达人,剧情搞笑,美妆
后端代码:
public function example(){
$where = [];
if(stristr($tag,',')){
$where['tags']=$this->strTool($tag);
}else{
$where['tags'] = ['like', "%" . $tag . "%"];
}
if(stristr($location,',')){
$where['province'] =$this->strTool($location);
}else{
$where['province'] = ['like', '%' . $location . '%'];
}
$res = db('user')->where($where)->select();
return json($res);
}
/**对同字段多条件查询
* $str 要拆分的字符串
* $handle 字段值
*/
public function strTool($str)
{
$strArr=explode(',',$str);
$data=[];
foreach ($strArr as $v) {
$data[]=['like', "%" . $v . "%"];
}
$temp[0]=$data;
$temp[1]='or';
return $temp;
}