当前位置:首页 > PHP教程 > PHP高级教程

fleaphp下不确定的多条件查询的巧妙解决方法

问题:例如,实现如下
$data = array(
'id' => $_post['id1'],
'name' => $_post['name1']
);
$posts = $this->_modelstudent->findall($data);
页面上有 id name 的文本框 可输入id查询 也可输入name查询 也可同时输入查询 ;

解决:写循环做判断
例子如下:
$conditions = null;
$fields = array('id', 'name', 'sex', 'phone');
foreach($fields as $each) {
if(!empty($_post[$each])) {
if($conditions) {
$conditions .= " and {$each}={$_post[$each]}";
} else {
$conditions .= "{$each}={$_post[$each]}";
}
}
}
【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!