我将php与codeigniter框架结合使用,在我的函数中使用filter,用户可以输入1个过滤器,或者用户可以输入4个输入(最多5个)

我将php与codeigniter框架结合使用,在我的函数中使用filter,用户可以输入1个过滤器,或者用户可以输入4个输入(最多5个),php,codeigniter,Php,Codeigniter,在我的表中有5行,我想根据输入从表中获取数据。这些行是姓名、年龄、身高、体重和班级。 如果用户输入年龄=12且体重=40,则应显示年龄为12且体重为40的人员。 如果用户仅输入name=jack,则应显示名为jack的人员。 如果用户输入所有五个条目,则所有条目都应匹配。 用户可以输入一个字段或全部五个字段。我建议使用这些输入构建一个关联数组,其中包含要应用的过滤器 $filters = array(); if (!empty($this->input->post('name'))

在我的表中有5行,我想根据输入从表中获取数据。这些行是姓名、年龄、身高、体重和班级。 如果用户输入年龄=12且体重=40,则应显示年龄为12且体重为40的人员。 如果用户仅输入name=jack,则应显示名为jack的人员。 如果用户输入所有五个条目,则所有条目都应匹配。
用户可以输入一个字段或全部五个字段。

我建议使用这些输入构建一个关联数组,其中包含要应用的过滤器

$filters = array();

if (!empty($this->input->post('name')))
    $filters['name'] = $this->input->post('name');

if (!empty($this->input->post('age')))
    $filters['age'] = $this->input->post('age');

if (!empty($this->input->post('height')))
    $filters['height'] = $this->input->post('height');

if (!empty($this->input->post('weight')))
    $filters['weight'] = $this->input->post('weight');

if (!empty($this->input->post('class')))
    $filters['class'] = $this->input->post('class');
然后,在构建查询时,只需迭代数组:

foreach ($filters as $key => $value) {
    $this->db->where($key, $value);
}

注意使用
empty()
代替isset或任何手动比较

我建议使用输入构建一个关联数组,其中包含要应用的过滤器

$filters = array();

if (!empty($this->input->post('name')))
    $filters['name'] = $this->input->post('name');

if (!empty($this->input->post('age')))
    $filters['age'] = $this->input->post('age');

if (!empty($this->input->post('height')))
    $filters['height'] = $this->input->post('height');

if (!empty($this->input->post('weight')))
    $filters['weight'] = $this->input->post('weight');

if (!empty($this->input->post('class')))
    $filters['class'] = $this->input->post('class');
然后,在构建查询时,只需迭代数组:

foreach ($filters as $key => $value) {
    $this->db->where($key, $value);
}

注意使用
empty()
代替isset或任何手动比较

你的问题是什么?你试过代码中的任何东西吗?我看不到问题,我看不到代码,我看不到对你的帮助:)你的问题是什么?你试过代码中的任何东西吗?我看不到问题,我看不到代码,我看不到对你的帮助:)