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

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

WBOY
Release: 2016-06-13 12:27:06
Original
1147 people have browsed it

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template