title: Using CodeIgniter to populate drop down lists in forms
body: <p>Being new to CI and PHP, especially arrays, I'm struggling with documentation and tutorials. Please don't point me to the documentation, I have been reading it all day.</p>
  
  <p>As I understand it, I need a view (form), controller and model. The model connects to my default DB connection via a class/function (my DB is where my drop-down list country data is stored), it returns an array, and finally the model parses the array to the form.</p>
  
  <p>Where I struggle is how to parse the array along to the view. Because I'm dealing with 3 files, to output one html file, context is really important, as is syntax. I asked this earlier today and nearly lost my mind as the two answers I got were fragmented and out of context. They were helpful but assumed I knew a lot more than I do.</p>
  
  <p>In other words, please be explicit, because a syntax oversight or knowledge assumption could have me scratching my head for hours. Thanks in advance!</p>
  
  <p>Here's my attempt which isn't working, basically whatever variable I use in the view returns a variable undefined error...</p>
  
  <p>View/Form...</p>
  
  <pre><code>&lt;body&gt;   
  &lt;?php echo form_open('form'); ?&gt; 
  
  &lt;h5&gt;Country&lt;/h5&gt;
  &lt;?php echo form_dropdown('', $data, '');?&gt;
  
  &lt;div&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/div&gt;    
  &lt;?php echo form_close() ?&gt;    
  &lt;/body&gt;
  </code></pre>
  
  <p>My controller...</p>
  
  <pre><code>&lt;?php
  
  class Form extends CI_Controller {
  
      function index()
      {
          $this-&gt;load-&gt;helper(array('form', 'url'));
          $this-&gt;load-&gt;library('form_validation');        
  
  
  
  
          $this-&gt;load-&gt;model('country');
          $data['country'] = $this-&gt;country-&gt;get_country();
          $this-&gt;load-&gt;view('myform', $data);
  
  
          if ($this-&gt;form_validation-&gt;run() == FALSE)
          {
              $this-&gt;load-&gt;view('myform');
          }
          else
          {
              $this-&gt;load-&gt;database();
  
              $sql = "INSERT INTO donations (country) VALUES (
              ".$this-&gt;db-&gt;escape($this-&gt;input-&gt;post('country')).";
  
              $this-&gt;db-&gt;query($sql);
  
              echo $this-&gt;db-&gt;affected_rows(); 
  
              $this-&gt;load-&gt;view('formsuccess');
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p>My model...</p>
  
  <pre><code>&lt;?php
  
  class Country extends CI_Model
  {
  
  function get_country() {
      $this-&gt;load-&gt;database();
      $sql = ('select * from countries');
      return $this-&gt;db-&gt;query($sql)-&gt;result();
      }
  }
  ?&gt;
  </code></pre>