使用CodeIgniter4的价格范围筛选器

wfveoks0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(134)

我试图创建一个搜索与多个过滤器,其中一个过滤器是一个预算或价格过滤器,我使用下拉菜单,而不是创建一个价格范围栏。
换句话说,我的预算过滤器是:-

  • 0-2500000
  • 250000-500000
  • 500000-10000000
  • 10000000-25000000
  • 25000000-50000000
  • 5亿及以上

这是我的代码,我需要帮助来修改它。模型中的控制器和SQL查询中的函数只接受单个预算(价格)值并显示结果,而不是接受预算(价格)范围并显示该预算(价格)范围下的结果。
我想要实现的是,当用户在搜索表单中选择预算(价格)范围时,它应该显示属于所选 * 预算范围 * 或 * 价格范围 * 的属性。

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();
        
    }
slhcrj9b

slhcrj9b1#

代替:

// ...

$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():结合数组法

相关问题