title: codeigniter - display data from data base in dropdown
body: <p>i am creating an application in codeigniter for the form we want country and state in drop down on change selection of country
  Controller is like</p>
  
  <pre><code>public function user()
  {
          $data   = array();
          $this-&gt;load-&gt;model('enquiry');
          $this-&gt;load-&gt;helper('url');
          $data['result'] = $this-&gt;enquiry-&gt;get_reqtutor();
          $data['countries'] = $this -&gt; country_model -&gt; get_countries();
          $this-&gt;load-&gt;view('Table\DemoHistory',$data);
  }
  
  function get_states($country) {
       $this-&gt;load-&gt;helper('url');
       $this-&gt;load-&gt;model('state_model');
       header('Content-Type: application/x-json; charset=utf-8');
       echo(json_encode($this-&gt;state_model-&gt;get_states($country)));
  }
  </code></pre>
  
  <p><strong>and model is like
  state_model</strong>`</p>
  
  <pre><code>&lt;?php
  class State_model extends CI_Model {
      public function __construct() {
           $this -&gt; load -&gt; database();
      }
      function get_states($country = null){
           $this-&gt;db-&gt;select('state_id, state_name');
           if($country != NULL){
               $this-&gt;db-&gt;where('country_id', $country);
           }
           $query = $this-&gt;db-&gt;get('state_master');
           $states = array();
           if($query-&gt;result()){
               foreach ($query-&gt;result() as $state) {
                   $cities[$state-&gt;state_id] = $state-&gt;state_name;
               }
               return $states;
           } else {
               return FALSE;
           }
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>country_model</p>
  
  <pre><code>&lt;?php
  class Country_model extends CI_Model {
  
      public function __construct() {
           $this -&gt; load -&gt; database();
      }
  
      function get_countries() {
           $this -&gt; db -&gt; select('country_id, country_name');
           $query = $this -&gt; db -&gt; get('country_master');
  
          $countries = array();
  
          if ($query -&gt; result()) {
               foreach ($query-&gt;result() as $country) {
                   $countries[$country -&gt; country_id] = $country -&gt; country_name;
               }
               return $countries;
          } else {
               return FALSE;
          }
       }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Ajax is like</p>
  
  <pre><code> &lt;script type="text/javascript"&gt;// &lt;![CDATA[
       $(document).ready(function(){
           $('#country').change(function(){ //any select change on the dropdown with id country trigger this code
               $("#cities &gt; 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: /mtb/Schedule/get_states/"+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(state_id,state_name) //here we're doing a foeach loop round each city with id as the key and city as the value
                       {
                           var opt = $('&lt;option /&gt;'); // here we're creating a new select option with for each city
                           opt.val(state_id);
                           opt.text(state_name);
                           $('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
                       });
                   }
  
               });
  
           });
       });
   // ]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>and view is </p>
  
  <pre><code>&lt;?php $countries['#'] = 'Please Select'; ?&gt;
  
  &lt;label for="country"&gt;Country: &lt;/label&gt;&lt;?php echo form_dropdown('country_id', $countries, '#', 'id="country"'); ?&gt;
   &lt;?php $cities['#'] = 'Please Select'; ?&gt;
  &lt;label for="city"&gt;State: &lt;/label&gt;&lt;?php echo form_dropdown('state_id', $cities, '#', 'id="cities"'); ?&gt;
  </code></pre>
  
  <p>i am getting country name while my state value is not comming in dropdown please help........</p>