我有几个复选框,从数据库中过滤我的元素,但我不知道如何应用过滤器,如价格与最小值和最大值,因为从我的观点,我可以传递到控制器只有一个值我的输入
例如:我想对元素
应用一个介于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;
}
注意:这只是一个工作的想法,我还没有测试过代码