使用CodeIgniter4的价格范围筛选器



我正在尝试创建一个带有多个过滤器的搜索,其中一个过滤器是预算或价格过滤器,我使用下拉列表而不是创建价格范围栏。

换句话说,我的预算过滤器是:-

  • 0-2500000
  • 25000-500000
  • 50-100000
  • 10000000-25000000
  • 2500000-50000000
  • 50000000及以上

这是我的代码,我需要帮助修改它。模型中的Controller和SQL Query中的函数只获取一个预算(价格(值并显示结果,而不是获取预算(价格(price((范围并显示该预算范围下的结果。

我想要实现的是,当用户在搜索表单中选择预算(价格(范围时,它应该显示属于所选预算范围

价格范围HTML表单

<div class="search__area-inner">
<?= form_open('/home/search_residential'); ?>
<div class="row">
<div class="col-12"><?= csrf_field(); ?></div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="budget[]" class="wide select_option">
<option data-display="Select Budget">Select Budget</option>
<option value="0-2500000">less than 25 lacs</option>
<option value="250000-500000">25 to 50 lacs</option>
<option value="500000-10000000">50 lacs to 1 cr</option>
<option value="10000000-25000000">1 cr to 2.5 cr</option>
<option value="25000000-50000000">2.5 cr to 5 cr</option>
<option value="50000000">5 cr +</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<select name="type" class="wide select_option">
<option data-display="Select Property Type">Select Property Type</option>
<option value="Apartments">Apartments</option>
<option value="Bungalow">Bungalow</option>
<option value="Row House">Row House</option>
<option value="Villas">Villas</option>
</select>
</div>
</div>
<div class="col-6 col-lg-4 col-md-4">
<div class="form-group">
<input type="text" class="form-control" id="inputLocation" name="city" value="" placeholder="Enter City" required>
</div>
</div>
<br>
<div class="mx-auto">
<div class="form-group">
<button class="btn btn-primary text-uppercase btn-block"> search 
<i class="fa fa-search ml-1"></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>

控制器代码

public function search_residential() {
$data['getfooter'] = $this->homeModel->getFooterInfo();
$data['getad'] = $this->homeModel->getCarousel();
$data['pagelist'] =  $this->pageModel->getPages();
$data['cities'] = $this->db->table('tblcity')->select('*')->get()->getResult(); 

$params = array(
'budget' => $this->request->getVar('budget'),
'type' => $this->request->getVar('type'),
'city' => $this->request->getVar('city'),

);
$data['search'] = $this->homeModel->getSearch($params);     
return view('searchresidential', $data);
}
**Model code**
//Searching residential data
public function getSearch($params) {

$budget = $params['budget'];
$type = $params['type'];
$city = $params['city'];
$builder= $this->db->table('tblresidential');
$builder->select('*');
$builder->where('1 = 1');
if ($budget != '') {
$builder->where('budget', $budget);
}
if ($type != '') {
$builder->where('type', $type);
}
if ($city != '') {
$builder->where('city', $city);
}
return $builder->get()->getResult();

}

而不是:❌

// ...
$builder->where('budget', $budget);
// ...

使用此:✅

// ...
foreach ($budget as $range) {
$builder->orWhere((function (string $range) {
$limits = array_map(
fn($limit) => floatval($limit),
explode("-", $range)
);
return match (count($limits)) {
1 => ['budget >=' => $limits[0]],
2 => ['budget >=' => $limits[0], 'budget <=' => $limits[1]],
default => ['1 =' => 1]
};
})($range));
}
// ...

资源:$builder->where():关联数组方法

相关内容

  • 没有找到相关文章

最新更新