我想弄清楚如何基本檢查在下拉框中選擇的內容的值,並返回符合數據庫條件的所有結果,即如果用戶在下拉菜單中選擇該選項Up to 5 years old
,數據庫應該是搜索並返回所有2009年或以後有CarAge
的汽車。根據下拉框/滑動條中的選定選項過濾結果?
我在我的控制器中的以下內容:
if ($this->input->post())
{
$carBrand = $this->input->post('brand');
$carModel = $this->input->post('model');
$carColor = $this->input->post('colour');
$carminPrice = $this->input->post('minPrice');
$carminEngine = $this->input->post('minEngine');
$carMiles = $this->input->post('miles');
$carAge = $this->input->post('age');
$maxPriceString = $this->input->post('maxPrice');
$maxEngineString = $this->input->post('maxEngine');
$searchKeywordString = $this->input->post('searchkeyword');
$this->load->model("cars");
$data['results'] = $this->cars->searchCars($carBrand, $carModel, $carColor, $carminPrice, $carminEngine, $carMiles, $carAge, $maxPriceString, $maxEngineString, $searchKeywordString);
$this->load->view('searchresults.php', $data);
}
else
{
$this->load->view('search.php'); //display success view
}
}
然後在我的模型,我有這樣的:
public function searchCars($carBrand, $carAge, $carModel, $carColor, $carminPrice, $carminEngine, $carMiles, $carAge, $maxPriceString, $maxEngineString, $searchKeywordString)
{
//Get the current Year
$currentYear = date('Y');
//Subtract the number of years we want to take off
$startYear = $currentYear - $carAge;
//Search the database where CarAge is greater than the start year
$this->db->where('CarYear >=', $startYear);
$this->db->like('CarMiles', $carMiles);
$this->db->like('CarMake', $carBrand);
$this->db->like('CarModel', $carModel);
$this->db->like('CarColor', $carColor);
$this->db->like('CarPrice', $minPriceString);
$this->db->like('CarEngine', $minEngineString);
$this->db->like('CarPrice', $maxPriceString);
$this->db->like('CarEngine', $maxEngineString);
$this->db->like('CarModel', $searchKeywordString);
$query = $this->db->get('cars');
return $query->result();
}
}
形式的年齡部位看起來是這樣的:
<label>Age:</label>
<select name="age">
<option value>Any</option>
<option value="1">Up to 1 year old</option>
<option value="2">Up to 2 years old</option>
<option value="3">Up to 3 years old</option>
<option value="4">Up to 4 years old</option>
<option value="5">Up to 5 years old</option>
<option value="6">Up to 6 years old</option>
<option value="7">Up to 7 years old</option>
<option value="8">Up to 8 years old</option>
<option value="9">Up to 9 years old</option>
<option value="10">Up to 10 years old</option>
<option value="11">Over 10 years old</option>
</select>
最低價格滑塊示例:
<li>
<label>Min. Price:</label>
<input class="slider" oninput="amount.value=rangeInput.value" type="range" id="rangeInput" name="minPrice" min="0" max="10000" value="0" step="500">
<span class="output">
$
<output name="amount" for="rangeInput">0</output>
</span>
</li>
我不完全確定如何基本檢查數據庫字段中與年齡相關的下拉列表的值,並根據此結果返回結果。發生什麼事是因爲我在搜索表單上有多個滑塊和下拉列表,沒有任何篩選器正在工作,即嘗試搜索時不返回任何結果。
如何存儲你的數據庫上車的年齡嗎?這是日期還是天數的整數? –
它存儲爲一年,即2009年'int(4)' – user3429767