codeigniter 过滤器数据库

codeigniter filter database

我有几个复选框可以从数据库中过滤我的元素,但我不知道如何应用带有最小值和最大值的价格过滤器,因为从我看来我只能传递给控制器我输入的一个值
例如:我想对一个元素应用 1000 到 1500 之间的价格过滤器
查看:

        <li><input type="checkbox" name="price[]" value="1000">  <1.000</li>
        <li><input type="checkbox" name="price[]" value="1500"><a href="#">1.000 - 1.500</a></li>
        <li><input type="checkbox" name="price[]" value="2000"><a href="#">1.500 - 2.000</a></li>
        <li><input type="checkbox" name="price[]" value="3000"><a href="#">2.000 - 3.000</a></li>
        <li><input type="checkbox" name="price[]" value="4000"><a href="#">3.000 - 4.000</a></li>
        <li><input type="checkbox" name="price[]" value="5000"><a href="#">4.000 - 5.000</a></li>
        <li><input type="checkbox" name="price[]" value="5000"><a href="#">  >5.000</a></li>  

控制器:

public function laptops()
{

    $filter = $this->input->post();



    $data['laptops_toate'] = $this->emag_model->laptops_toate_grid($filter);


    $this->load->view('emag/laptops',$data);  

型号:

    public function laptops_toate_grid($filter = null){
    $this->db->select('*')
             ->from('laptop_notebook')
            ->join('brands','brands.id_brands = laptop_notebook.brand');


    if($filter['price']){
        $this->db->where('price <', $filter['price']);
    }


    $query = $this->db->get()->result();



    return $query;

}

只是想知道如何做。

查看文件:

<li><input type="checkbox" name="price[]" value="<1000">  <1.000</li>
<li><input type="checkbox" name="price[]" value="1000-1500"><a href="#">1.000 - 1.500</a></li>
<li><input type="checkbox" name="price[]" value="1500-2000"><a href="#">1.500 - 2.000</a></li>
<li><input type="checkbox" name="price[]" value="2000-3000"><a href="#">2.000 - 3.000</a></li>
<li><input type="checkbox" name="price[]" value="3000-4000"><a href="#">3.000 - 4.000</a></li>
<li><input type="checkbox" name="price[]" value="4000-5000"><a href="#">4.000 - 5.000</a></li>
<li><input type="checkbox" name="price[]" value=">5000"><a href="#">  >5.000</a></li>

控制器保持不变。
模型变为:

public function laptops_toate_grid($filter = null){
    $this->db->select('*')
             ->from('laptop_notebook')
            ->join('brands','brands.id_brands = laptop_notebook.brand');

    if (isset($filter['price'])) { // make sure there is price key
        $filters = array(); // this is where we store the filters
        foreach ($filter['price'] as $price) { // iterate through prices
            if ($filter['price'][0] == '<') { // is less
                $filters[] = 'price < ' . (int)substr($price, 1);
            }
            elseif ($filter['price'][0] == '>') { // is more
                $filters[] = 'price > ' . (int)substr($price, 1);
            }
            else { // is between
                $expl = explode('-', $price);
                $filters[] = 'price BETWEEN ' . (int)$expl[0] . ' AND ' . (int)$expl[1];
            }
        }
        if ($filters) { // if there are filters, create the query
            $query = implode(' OR ', $filters);
            $query = '('. $query . ')';
            $this->db->where($query, false); // false makes sure the query is not escaped
        }
    }

    $query = $this->db->get()->result();

    return $query;
}

注意:这只是一个可行的想法,我还没有测试过代码。