2014-12-24 93 views
-1

相關的下拉列表中沒有工作依賴的下拉列表中笨

我控制器

<?php 
class User extends CI_Controller { 

public function __construct() { 
parent::__construct(); 
$this -> load -> model('country_model'); 

} 

function index() 
{ 

$data['countries'] = $this -> country_model -> get_countries(); 
$this -> load -> view('post_view', $data); 
} 

function get_cities($country){ 
$this->load->model('city_model'); 
header('Content-Type: application/x-json; charset=utf-8'); 
echo(json_encode($this->city_model->get_cities($country))); 
} 

} 

模型

<?php 
class Country_model extends CI_Model { 

public function __construct() { 
$this -> load -> database(); 

} 

function get_countries() { 
$this -> db -> select('id, country_name'); 
$query = $this -> db -> get('countries'); 

$countries = array(); 

if ($query -> result()) { 
foreach ($query->result() as $country) { 
$countries[$country -> id] = $country -> country_name; 
} 
return $countries; 
} else 
    { 
return FALSE; 
} 
} 

} 

我的城市模型

<?php 
class City_model extends CI_Model { 

public function __construct() { 
$this -> load -> database(); 

} 

function get_cities($country = null){ 
$this->db->select('id, city_name'); 

if($country != NULL){ 
$this->db->where('country_id', $country); 
} 

$query = $this->db->get('cities'); 

$cities = array(); 

if($query->result()){ 
foreach ($query->result() as $city) { 
$cities[$city->id] = $city->city_name; 
} 
return $cities; 
}else{ 
return FALSE; 
} 
} 

} 
?> 

和我查看(post_view)

<html> 

<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript">// <![CDATA[ 
$(document).ready(function(){ 
$('#country').change(function(){ //any select change on the dropdown with id country trigger this code 
$("#cities > option").remove(); //first of all clear select items 
var country_id = $('#country').val(); // here we are taking country id of the selected one. 
$.ajax({ 
type: "POST", 
url: "Co/"+country_id, //here we are calling our user controller and get_cities method with the country_id 

success: function(cities) //we're calling the response json array 'cities' 
{ 
$.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value 
{ 
var opt = $('<option />'); // here we're creating a new select option with for each city 
opt.val(id); 
opt.text(city); 
$('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities' 
}); 
} 

}); 

}); 
}); 
// ]]> 
</script> 
</head> 
<body> 


    <form method="get"> 
    <label for="category">Parent Category</label> 
    <select name="countries" id="parent_cat"> 
    <?php foreach($countries as $m) 
    { 
     ?> 
     <option value="<?php echo $m->id; ?>"><?php echo $m->category_name; ?></option> 
     <?php 
    } 
    ?> 
    </select> 
</form> 
<?php $countries['#'] = 'Please Select'; ?> 

<label for="country">Country: </label> 

<?php form_dropdown('country_id', $countries, '#', 'id="country"'); ?><br /> 


<?php $cities['#'] = 'Please Select'; ?> 
<label for="city">City: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br /> 
</body> 
</html 

我國分貝

id country_name 
1 India 
2 Sri Lanka 

我的城市分貝

id city_name country_id 
1 Mumbai 1 
2 Kolkatta 1 
3 Colombo  2 
4 Matara 2 
+0

好的。那麼你能解釋一下這個給我們多一點嗎? 「不工作」是什麼意思? *什麼*不起作用?你想做什麼?與你期望的相比,你看到了什麼?你有沒有嘗試過任何調試?你看到任何錯誤/警告消息嗎?作爲參考,只需粘貼所有代碼並說「不起作用」不是一個好問題。你需要解釋你的問題,所以我們可以幫助你。 –

+0

在此代碼錯誤是..下拉列表不顯示在視圖頁面 –

回答

0

因爲你沒有創建它,這一行添加到您的意見/ post_view.php文件:

<?php echo form_dropdown('country_id', $countries, '') ?>