title: Dynamic drop down from database in codeigniter
body: <p>I have two dropdowns, I want to fill (<code>deviceType</code>) from table <code>device_typt_table</code> with two columns <code>id</code>, <code>device_type</code> and (<code>brand</code>) from <code>brand_table</code> with <code>id, brand_name, device_type_id</code>.</p>
  
  <p>I try to do that and print data with controller but can't print it in select box
  but can print in my view Except select box</p>
  
  <p>My view:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;/body&gt;
  &lt;?php echo validation_errors(); ?&gt;
  &lt;?php echo form_open('speed/insert_to_db'); ?&gt;
  
  
  Branch: &lt;input type="text"  name="branch" /&gt;&lt;br/&gt;
  Business Unit: &lt;input type="text"  name="buinessUnit" /&gt;&lt;br/&gt;
  
  Device Type:&lt;select name="deviceType" &gt;
                 &lt;? foreach $devices as $post { ?&gt;
                &lt;option value="&lt;?php echo $post-&gt;device_type;?&gt;"&gt;&lt;?php echo $post-&gt;device_type;?&gt;&lt;/option&gt;
                  &lt;? }?&gt;
            &lt;/select&gt;&lt;br/&gt;
  Brand:&lt;select name="brand" &gt;
  
            &lt;/select&gt;&lt;br/&gt;
  Device Model: &lt;input type="text"  name="deviceModel" /&gt;&lt;br/&gt;
  SN: &lt;input type="text"  name="SN" /&gt;&lt;br/&gt;
  status: &lt;input type="text"  name="status" /&gt;&lt;br/&gt;
  department: &lt;input type="text"  name="department" /&gt;&lt;br/&gt;
  username: &lt;input type="text"  name="username" /&gt;&lt;br/&gt;
  notes: &lt;input type="textarea"  name="notes" /&gt;&lt;br/&gt;
  computername: &lt;input type="text"  name="computerName" /&gt;&lt;br/&gt;
  Save:&lt;input type="submit"  name="submit" value="save" /&gt;
  
  
  &lt;?php foreach($devices as $post){?&gt;
  &lt;?php echo $post-&gt;device_type;?&gt;
  &lt;?php }?&gt;  
  
  
  &lt;?php echo form_close(); ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>    &lt;?php
      class add_model extends CI_Model {
  
             public function insert_into_db(){
                 $post=$this-&gt;input-&gt;post();
                 $data=array('Branch'=&gt;$post['branch'],'BusinessUnit'=&gt;$post['buinessUnit'],'DeviceType'=&gt;$post['deviceType'],'Brand'=&gt;$post['brand'],'DeviceModel'=&gt;$post['deviceModel'],'SN'=&gt;$post['SN'],'Status'=&gt;$post['status'],'Departmant'=&gt;$post['department'],'UserName'=&gt;$post['username'],'Notes'=&gt;$post['notes'],'ComputerName'=&gt;$post['computerName']);
                 $this-&gt;db-&gt;insert('hardware_assets', $data);
      return $this-&gt;db-&gt;insert_id(); // if using mysql
  
             }
      function all_devices()
      {
       $this-&gt;db-&gt;select("id,device_type");
        $this-&gt;db-&gt;from('device_type_table');
        $query = $this-&gt;db-&gt;get();
        return $query-&gt;result();
      } 
  
  
       }
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>&lt;?php
  
  class Speed extends CI_Controller {
  
          function insert_to_db()
             {
                 $this-&gt;load-&gt;helper(array('form', 'url'));
                $this-&gt;load-&gt;library('form_validation');
                 $this-&gt;form_validation-&gt;set_rules('branch', 'Branch', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('buinessUnit', 'BuinessUnit', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('deviceType', 'DeviceType', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('brand', 'Brand', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('deviceModel', 'DeviceModel', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('SN', 'SN', 'trim|required|xss_clean|is_unique[hardware_assets.SN]');
                 $this-&gt;form_validation-&gt;set_rules('status', 'Status', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('department', 'Department', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('notes', 'Notes', 'trim|required|xss_clean');
                 $this-&gt;form_validation-&gt;set_rules('computerName', 'ComputerName', 'trim|required|xss_clean');
              /*   
  
             */
  $this-&gt;data['devices'] = $this-&gt;add_model-&gt;all_devices(); // calling Post model meAthod all_devices()
     $this-&gt;load-&gt;view('pages/home', $this-&gt;data);
  
             if ($this-&gt;form_validation-&gt;run() == FALSE)
               { 
  
               $this-&gt;load-&gt;view('pages/home');//loading success view     
           }
           else
           {
               $formSubmit = $this-&gt;input-&gt;post('submit');
                   if( $formSubmit == 'save' ){
                       $this-&gt;add_model-&gt;insert_into_db();
                       //loading success view
                       redirect('speed/insert_to_db');
                      }
               }
             }
  }
  </code></pre>