title: populate dropdown from database in codeigniter
body: <p>I have a table in that table have 3 field (category1,category2,category3).Now i want to populate the Dropdown if dropdown one select ,dropdown 2 values will genarat from the data base . Can any one help me.</p>
  
  <p>My Controller</p>
  
  <pre><code>            $this-&gt;load-&gt;model('quiz_mode');
              $arrStates = $this-&gt;quiz_mode-&gt;get_unique_catg1();
  
              foreach ($arrStates as $states)
              {
                  $arrFinal[$category-&gt;category1 ] = $category-&gt;category1 ;
              }
              $data['category2'] = $arrFinal;
        function ajax_call() 
      {
  
          //Checking so that people cannot go to the page directly.
          if (isset($_POST) &amp;&amp; isset($_POST['category1']))
          {
  
              $catogory = $_POST['category1'];
              $arrcategory = $this-&gt;sampl_mode-&gt;get_cities_from_catgo1($catogory);
  
              foreach ($arrcategory as $category)
              {
                 $arrFinal[$category-&gt;category2] = $category-&gt;category2;
              }
  
              //Using the form_dropdown helper function to get the new dropdown.
              print form_dropdown('category2',$arrFinal);
          }
          else
          {
              redirect('logged_in_user'); //Else redire to the site home page.
          }
      }
  </code></pre>
  
  <p>My Model</p>
  
  <pre><code>function get_unique_catg1()
  {
  
      $query = $this-&gt;db-&gt;query("SELECT DISTINCT category1  FROM sampl_table");
  
      if ($query-&gt;num_rows &gt; 0) {
          return $query-&gt;result();
      }
  }
      function get_cities_from_catgo1($category) {
      $query = $this-&gt;db-&gt;query("SELECT category2  FROM sampl_table WHERE category2 = '{$category}'");
  
      if ($query-&gt;num_rows &gt; 0) {
          return $query-&gt;result();
      }
  }
  </code></pre>
  
  <p>My view</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; 
      &lt;title&gt;untitled&lt;/title&gt;
  
          &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              $(document).ready(function () {
  
                  $('#catgo1-dropdown').change(function () {
                      var selCat1 = $('#catgo1-dropdown').val();
                      console.log(selCat1);
                      $.ajax({
                          url: "&lt;?php echo base_url();?&gt;index.php/sample/ajax_call", //The url where the server req would we made.
                          async: false,
                          type: "POST", //The type which you want to use: GET/POST
                          data: "category1="+selCat1, //The variables which are going.
                          dataType: "html", //Return data type (what we expect).
  
                          //This is the function which will be called if ajax call is successful.
                          success: function(data) {
                              //data is the html of the page where the request is made.
                              $('#cotegory').html(data);
                          }
                      })
                  });
              });
          &lt;/script&gt;
  &lt;/head&gt;
  
      &lt;body&gt;
      &lt;div id="wrapper"&gt;
                  &lt;div id="catgo1-dropdown"&gt;&lt;?php print form_dropdown('category1'); ?&gt;&lt;/div&gt;
                  &lt;div id="cotegory2"&gt;&lt;/div&gt;
  
              &lt;/div&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>