CodeIgniter 4 - 无法显示带有多个下拉列表的搜索结果

CodeIgniter 4 - Cannot display search result with multiple dropdown

对于我的网站,我想使用下拉列表 select 输入中的参数显示搜索结果(在一个全新的页面上)。我有 4 个下拉菜单,我尝试了多种不同的方法来使其工作。但是好像不行!

下面是我的 CodeIgniter 4 代码

家庭控制器

家庭控制器文件从 4 个下拉输入中收集数据,数据以数组形式获取。然后通过位于 home 模型中的 getSearch 方法执行该数组。

public function search_residential() {

    $data['getfooter'] = $this->homeModel->getFooterInfo();

    $params = array(

            'category' => $this->request->getVar('category'),
            'area' => $this->request->getVar('area'),
            'bedrooms' => $this->request->getVar('bedrooms'),
            'washrooms' =>$this->request->getVar('washrooms'),  
                      
            );

    $data['search'] = $this->homeModel->getSearch($params);     

    return view('search', $data);
}

家居模型

主页模型包含搜索查询。

//Searching data
public function getSearch($params) {

    
    $category = $params['category'];
    $area = $params['area'];
    $bedrooms = $params['bedrooms'];
    $washrooms = $params['washrooms'];

    $builder= $this->db->table('tblresidential');
    $builder->select('*');
    $builder->where('1 = 1');

    if ($category != '') {

        $builder->where('category', $category);

    }
    if ($area != '') {

        $builder->where('area', $area);

    }
    if ($bedrooms != '') {

        $builder->where('bedrooms', $bedrooms);

    }
    if ($washrooms != '') {

         $builder->where('washrooms', $washrooms);

    }

    $result = $builder->get();
    return $result;
    
}

HTML代码

用于执行的HTML代码

<div class="search__area-inner">
        <?= form_open('/home/search_residential'); ?>
            <div class="row">
                <div class="col-6 col-lg-3 col-md-3">
                    <div class="form-group">
                        <select name="category" class="wide select_option">
                            <option data-display="Property Type">Property Type</option>
                            <option value="Property on sale">Property on Sale</option>
                            <option value="Property on rent">Property on Rent</option>

                        </select>
                    </div>
                </div>
                <div class="col-6 col-lg-3 col-md-3">
                    <div class="form-group">
                        <select name="area" class="wide select_option">
                            <option data-display="Select Area">Select Area</option>
                            <option value="2000">2000</option>
                            <option value="1800">1800</option>
                            <option value="1500">1500</option>
                            <option value="1200">1200</option>
                            <option value="900">900</option>
                            <option value="600">600</option>
                            <option value="300">300</option>
                            <option value="100">100</option>
                        </select>
                    </div>
                </div>
                <div class="col-6 col-lg-3 col-md-3">
                    <div class="form-group">
                        <select name="bedrooms" class="wide select_option">
                            <option data-display="Select Bedrooms">Select Bedrooms</option>
                            <option value="1 bedroom">1</option>
                            <option value="1.5 bedrooms">1.5</option>
                            <option value="2 bedrooms">2</option>
                            <option value="2.5 bedrooms">2.5</option>
                            <option value="3 bedrooms">3</option>
                            <option value="4 bedrooms">4</option>
                            <option value="5 bedrooms">5</option>
                        </select>
                    </div>
                </div>
                <div class="col-6 col-lg-3 col-md-3">
                    <div class="form-group">
                        <select name="washrooms" class="wide select_option">
                            <option data-display="Select Washrooms">Washrooms</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                        </select>
                    </div>
                </div>
                <div class="col-12">
                    <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>

结果视图(显示结果的页面)

这是显示结果的页面

    <?php if(!empty($search)) : ?>
<section class="residential-property-list">
        <div class="container">
        <div class="row">
        <?php foreach($search as $properties) : ?>
                <div class="col-lg-12">
                <div class="card__image card__box-v1">
                <div class="row no-gutters">
                        <div class="col-md-4 col-lg-3 col-xl-4">
                                <div class="card__image__header h-250">
                                        <a href="<?= base_url('/home/residential_property'); ?>/<?= $properties->id; ?>">
                                        <img src="<?= $properties->imageone; ?>" alt="featured-image" class="img-fluid w100 img-transition">
                                        <div class="info text-white"> for <?= $properties->category; ?></div>
                                        </a>
                                </div>
                                </div>
                                <div class="col-md-4 col-lg-6 col-xl-5 my-auto">
                                <div class="card__image-body">
                                    <h6 class="text-capitalize">
                                    <?= $properties->project; ?>
                                    </h6>
                                    <ul class="list-inline card__content">
                                        <li class="list-inline-item">
                                            <span>
                                                Bedrooms <br>
                                                <i class="fa fa-inbox"></i> <?= $properties->bedrooms; ?>
                                            </span>
                                        </li>
                                        <li class="list-inline-item">
                                            <span>
                                                Washrooms <br>
                                                <i class="fa fa-map"></i> <?= $properties->washrooms; ?>
                                            </span>
                                        </li>
                                        <li class="list-inline-item">
                                            <span>
                                                Property Facing <br>
                                                <i class="fa fa-inbox"></i> <?= $properties->facing; ?>
                                            </span>
                                        </li>
                                        <li class="list-inline-item">
                                            <span>
                                                Property area <br>
                                                <i class="fa fa-map"></i> <?= $properties->area; ?>
                                            </span>
                                        </li>
                                    </ul>
                                </div>
                                </div>
                                </div>
                                </div>

                                </div>
                <?php endforeach; ?>
        </div>
</section>
<?php else: ?>
        <br>
        <br>
        <div class="container"><h4 class="text-center">No Result Found Yet</h4></div>
        <br>
        <br>
<?php endif; ?>

模型的get(...)方法returnsfalse|ResultInterface.

接收实际结果集,

而不是:

(家居模型)

// ...

    $result = $builder->get();
    return $result;

// ...

使用这个:

Result Arrays

// ...

    return $builder->get()->getResult();

// ...