我试图找出如何基本检查下拉框中所选内容的值,并从数据库中返回符合条件的所有结果,即如果用户在下拉列表中选择选项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>
我不完全确定如何在数据库字段中基本检查下拉时间的值,并根据此返回结果。发生的事情是因为我在搜索表单上有多个滑块和下拉列表,没有一个过滤器正在工作,即在尝试搜索时没有返回任何结果。
答案 0 :(得分:0)
您想要更改模型功能以减去用户从当前年份中选择的年数。然后,一旦你有了开始年份,你想在数据库中搜索年龄大于开始年份的任何汽车。
以下是一个例子
public function searchCars($carAge, $carMiles)
{
//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('CarAge >=', $startYear);
$this->db->like('CarMiles', $carMiles);
$query = $this->db->get('cars');
return $query->result();
}