我有一个包含四个下拉列表的搜索页面,其中设置的值用于查询数据库。我已经测试过以确保将值传递给模型,但数据库返回空数组。但是,如果我直接在模型中的sql查询中输入值,它将返回结果集。以下是我的代码。提前谢谢。
控制器
public function search(){
$this->form_validation->set_rules('state', 'State', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
$this->form_validation->set_rules('minPrice', 'Minimum Price', 'required');
$this->form_validation->set_rules('maxPrice', 'Maximum Price', 'required');
if ($this->form_validation->run() == FALSE) {
$this->index();
}
else{
$state = $this->input->post('state');
$city = $this->input->post('city');
$minPrice = $this->input->post('minPrice');
$maxPrice = $this->input->post('maxPrice');
$data['hostel_search'] = $this->Hostel_model->search_hostels($state, $city, $minPrice, $maxPrice);
$this->load->view('_parts/header');
$this->index_top();
$this->load->view('search', $data);
$this->load->view('_parts/footer');
}
}
模型
public function search_hostels($state, $city, $minPrice, $maxPrice){
$state = (int)$state;
$city = (int)$city;
$minPrice = (int)$minPrice;
$maxPrice = (int)$maxPrice;
$query = $this->db->query("SELECT * FROM `hostels` WHERE `state_id`='$state' AND `city_id`='$city' AND `price` BETWEEN '$minPrice' AND '$maxPrice'");
$this->db->from($this->table);
$result = $query->result();
return $result;
}
查看
<?php echo form_open('site/search'); ?>
<div class="col-sm-4">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa"><strong>State</strong></i></span>
<select id="state" name="state" class="form-control" required>
<option value="" selected>--Select State--</option>
<?php foreach ($state_option as $state_id => $state) { ?>
<option id="<?php echo $state_id; ?>" value="<?php echo $state_id; ?>"><?php echo $state; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-sm-4">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa"><strong>City</strong></i></span>
<select id="city" name="city" class="form-control" required>
<option id="" value="" selected>--Select City--</option>
</select>
</div>
</div>
<div class="col-sm-4">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa"><strong>Purpose</strong></i></span>
<select name="purpose" class="form-control" required>
<option value="" selected>--Select Purpose--</option>
<option value="rent">Rent</option>
<option value="let">Let</option>
</select>
</div>
</div>
<div class="col-sm-4">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa"><strong>Min Price</strong></i></span>
<select name="minPrice" class="form-control" required>
<option value="" selected>--Select Price--</option>
<option value="5000">5000</option>
<option value="7000">7000</option>
<option value="8000">8000</option>
<option value="9000">9000</option>
<option value="10000">10000</option>
</select>
</div>
</div>
<div class="col-sm-4 pull-center" >
<button class="btn btn-block btn-info btn-md" type="submit" style="margin-bottom:20px; background-color:#06F; border-radius:5px; border:none; position: relative;" id="search"><i class="fa fa-search"></i> Search</button>
</div>
<div class="col-sm-4">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa"><strong>Max Price</strong></i></span>
<select name="maxPrice" class="form-control" required>
<option value="" selected>--Select Price--</option>
<option value="50000">50000</option>
<option value="60000">60000</option>
<option value="70000">70000</option>
<option value="80000">80000</option>
<option value="100000">100000</option>
</select>
</div>
</div>
<?php echo form_close();?>
AM使用此JavaScript根据状态ID
中返回的值填充城市$(document).ready(function(){
$('#state').change(function(){
$('#city').html('<option>Loading....</option>');
var state = $('#state').val();
if (state != "") {
$.get("<?php echo base_url(); ?>site/dependent_dropdown/"+state).success(function(data){
$('#city > option').remove();
data = JSON.parse(data);
for(var i in data){
$('#city').append("<option id='"+i+"' value='"+data+"'>"+data[i]+"</option>");
}
})
}
else{
$('#city').html('<option>--Select City--</option>');
}
});
});
请不要担心$this->index_top()
。这是为了它的目的。
答案 0 :(得分:1)
我的JavaScript就是问题所在
我换了这条线
$('#city').append("<option id='"+i+"' value='"+data+"'>"+data[i]+"</option>");
对此
$('#city').append("<option value='"+i+"'>"+data[i]+"</option>");
非常感谢@Goose,调试语法是线索。我注意到,在我的索引页面中,$city_id
正在返回0
。但是从搜索页面中获取了适当的值。然后萤火虫来玩,其余的都是历史。
如果您发现我的问题和答案有帮助,请将其投票。
谢谢你们所有人。