For my website I want to display search result (on a complete new page) using the parameters in the dropdown select input. I have 4 dropdowns and I tried multiple different ways to make it work. But it doesn't seem to work!
Below is my CodeIgniter 4 code
Home Controller
The home controller file which gathers data from 4 dropdown inputs the data is fetched as an array. The array is then executed through the getSearch method located in the home model.
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 Model
The home model contains the query of search.
//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 Code
The HTML code that is used for execution
<div >
<?= form_open('/home/search_residential'); ?>
<div >
<div >
<div >
<select name="category" >
<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 >
<div >
<select name="area" >
<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 >
<div >
<select name="bedrooms" >
<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 >
<div >
<select name="washrooms" >
<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 >
<div >
<button > search
<i ></i>
</button>
</div>
</div>
</div>
<?= form_close(); ?>
</div>
Result View (the page on which result shows)
This is the page where result shows
<?php if(!empty($search)) : ?>
<section >
<div >
<div >
<?php foreach($search as $properties) : ?>
<div >
<div >
<div >
<div >
<div >
<a href="<?= base_url('/home/residential_property'); ?>/<?= $properties->id; ?>">
<img src="<?= $properties->imageone; ?>" alt="featured-image" >
<div > for <?= $properties->category; ?></div>
</a>
</div>
</div>
<div >
<div >
<h6 >
<?= $properties->project; ?>
</h6>
<ul >
<li >
<span>
Bedrooms <br>
<i ></i> <?= $properties->bedrooms; ?>
</span>
</li>
<li >
<span>
Washrooms <br>
<i ></i> <?= $properties->washrooms; ?>
</span>
</li>
<li >
<span>
Property Facing <br>
<i ></i> <?= $properties->facing; ?>
</span>
</li>
<li >
<span>
Property area <br>
<i ></i> <?= $properties->area; ?>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<?php else: ?>
<br>
<br>
<div ><h4 >No Result Found Yet</h4></div>
<br>
<br>
<?php endif; ?>
CodePudding user response:
The model's get(...)
method returns false|ResultInterface
.
To receive the actual result set,
Instead of:
(Home Model)
// ...
$result = $builder->get();
return $result;
// ...
Use this:
// ...
return $builder->get()->getResult();
// ...