I am trying to populate dropdown with database what i am actually trying to do i have 3 drop downs country ,state and city and i want if user select any country crossponding states are shown in dropdown and crossponding cities releated to state are shown in city dropdown.I don't how to achieve this all fields will be saved in tabl_usrs and and i have nm_country,nm_state and nm_city table from where country states and cities are shown in tabl_usrs i have country,state and city field wwhich act as foriegn key??
any links will be welcome.Hope any one can help.below is just i am trying to do this seprate from project


$data['city_list'] = $this->City_model->get_dropdown_list();
$this->load->view('my_view_file', $data); 

function get_dropdown_list()
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[$row['id']] = $row['name'];

        return $return;


<?php echo form_dropdown('city_id', $city_list, set_value('city_id', $city_id));
3 Years
Discussion Span
Last Post by anmol.raghuvanshi1

You have 24 lines of code (along with empty lines) and I could make more than 30 fast comments of why this MVC implementation has serious problems.

But lets stay in your question , do you know how to perform an AJAX request?

Do you know how dynamically change the options of a select client side?

Do you know how to make that select visible or not visible client side?

//my view performing some ajax request
                        $(document).ready(function() {  
                     /*dropdown post *///  
                        url:"<?php echo  
                        data: {id:  
                        type: "POST",  
                    <label for="country">Country</label>
                         <?php echo form_dropdown('country',$country,'','class="required" id="country" '); ?>
                         <br />
                         <br />

                        <label for="state">State</label>
                         <select name="state" id="state">  
                             <option value="">Select</option>  
                        <label for="city">City</label>
                          <option>Select City</option>
                          <option value="volvo">Volvo</option>
                          <option value="saab">Saab</option>
                          <option value="mercedes">Mercedes</option>
                          <option value="audi">Audi</option>

my controller

//my controller 
       class Country_state extends CI_Controller {  
       public function __construct()  
       public function index()  
          //starts by running the query for the countries dropdown  
          $data['countryDrop'] = $this->Country_states_cities->getCountries();  
          //loads up the view with the query results  
          $this->load->view('camping_registration', $data);  
       //call to fill the second dropdown with the states 
       public function buildDropStates()  

          //set selected country id from POST  
          echo $id_country = $this->input->post('id');  
          //run the query for the cities we specified earlier  
          $output = null;  
          foreach ($districtData['districtDrop'] as $row)  
             //here we build a dropdown item line for each  query result  
             $output .= "<option value='".$row->sub_name."'>".$row->sub_name."</option>";  
          echo $output;  
         // var_dump($output);

my model

    class Country_states_cities extends CI_Model {  
       public function __construct()  
       //fill your contry dropdown  
       public function getCountries()  
          $query = $this->db->get(); 
          // the query mean select country_id,country_name from  nm_country
          foreach($query->result_array() as $row){  
          // the fetching data from database is return  
          return $data;

       //fill your state dropdown depending on the selected country  
       public function getCityByCountry($country_id)  
         // $this->db->from('nm_state');  
         // $this->db->where('country_id',$country_id);  
          $query =  $this->db->query("SELECT state_id,state_name FROM `nm_state` WHERE country_id='$country_id'");
          $result = $query->result_array();  
          return $result;  


after more learning and doing i have above code but it's problem is returning array is empty i can't able to figure out why??

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.