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

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

对于我的网站,我想显示搜索结果(在一个完整的新页面上)使用下拉选择输入的参数。我有4个下拉,我尝试了多种不同的方法来使它工作。但它似乎不工作!
下面是我的CodeIgniter 4代码

主控制器

从4个下拉输入中收集数据的家庭控制器文件,数据被提取为一个数组。然后通过家庭模型中的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);
}

家庭模式

home模型包含要搜索的查询。

//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; ?>
bcs8qyzn

bcs8qyzn1#

模型的get(...)方法会传回false|ResultInterface
要接收实际的结果集,
代替:

  • (家用机型)*
// ...

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

// ...

使用此选项:
结果数组

// ...

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

// ...

相关问题