我想有三个国家,州和城市的下拉唐斯,我想从数据库中填充它们。我成功地检索了国家下拉列表的值,但问题是州下拉列表中的值根据选定的国家显示。作为一个例子,如果我选择U.S.A,我只想得到美国的州,我有两个表countries
和states
,我有country_id
作为state
表中的外键,实际上这就是我的问题所在。
我的代码-控制器:
// call to fill the second dropdown with the states
public function buildDropStates()
{
// set selected country id from POST
echo $id_country = $this->input->post('country_id');
// var_dump($id_country);
// run the query for the cities we specified earlier
$districtData['districtDrop']=$this->Country_states_cities->getCityByCountry($id_country);
// var_dump($districtData);
$output = null;
foreach ($districtData['districtDrop'] as $row)
{
// here we build a dropdown item line for each query result
$output .= "<option value='".$row->state_id."'>".$row->state_name."</option>";
}
echo $output;
// var_dump($output);
}
字符串
我的模型
//fill your state dropdown depending on the selected country
public function getCityByCountry($country_id=string)
{
$this->db->select('state_id,state_name');
$this->db->from('nm_state');
$this->db->where('country_id',$country_id);
$query = $this->db->get();
//var_dump($query);
var_dump($query->result());
return $query->result();
}
}
型
这是一个观点:
<script>
$(document).ready(function() {
$("#country").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo
base_url();?>Country_state/buildDropStates",
data: {id:
$(this).val()},
type: "POST",
success:function(data){
$("#state").html(data);
}
});
});
});
</script>
<label for="country">Country</label>
<?php echo form_dropdown('country',$countryDrop,'','class="required" id="country" '); ?>
<br />
<br />
<label for="state">State</label>
<select name="state" id="state">
<option value="">Select</option>
</select>
型
问题是我的模型getCityByCountry()返回一个大小为0的空数组。
当我从模型中注解下面一行时,它返回了DB中的所有值,那么这里到底发生了什么?是外键约束不起作用还是其他原因?
$this->db->where('country_id',$country_id);
型
回显查询时
CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 2 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.24 [server_version] => 50624 [stat] => Uptime: 2694 Threads: 1 Questions: 6365 Slow queries: 0 Opens: 88 Flush tables: 1 Open tables: 81 Queries per second avg: 2.362 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 715 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 2 [lengths] => [num_rows] => 0 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )
型
2条答案
按热度按时间ghhaqwfi1#
型号
字符串
控制器
型
yzckvree2#
您正在回显$id_country,并且函数getCityByCountry没有获取参数,因此没有返回结果,请删除回显
字符串