Home  >  Article  >  php教程  >  fleaphp下不确定的多条件查询的巧妙解决方法

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

WBOY
WBOYOriginal
2016-06-13 12:27:061129browse

问题:例如,实现如下
$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]}";
}
}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn