title: Trying to get property of non-object? PHP/Codeigniter
body: <p>I have been literally pulling my hair out with this one and its beginning to delay the rest of my project and it really is getting me down.</p>
  
  <p>I am trying to populate a pull down using values taken from a database table so that if in the future the user wants to add more options to the pull down they can add them to the table in the database.</p>
  
  <p><strong>I am using the Codeigniter platform (PHP) using MVC design pattern.</strong></p>
  
  <p>Here is the error message I am getting:</p>
  
  <blockquote>
    <p>A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/submit.php Line Number: 139</p>
  </blockquote>
  
  <p><strong>My Model function is this here which retrieves the rows from the table called "Staff".
  This works fine!</strong></p>
  
  <pre><code>function retrieve_values()
  {
      $query = $this-&gt;db-&gt;get('staff');
  
      if ($query-&gt;num_rows() &gt; 0) 
      { 
          //true if there are rows in the table
          return $query-&gt;result_array(); //returns an object of data
      }
  
      return false;
  }
  </code></pre>
  
  <p><strong>This is the controller function which receives the parameter and passes it to my view.
  This works fine!</strong></p>
  
  <pre><code>public function displayform()
  {
  
      //Checks if a user is logged in, if they are not they get redirected -
      if ( $this-&gt;session-&gt;userdata('name') == FALSE || $this-&gt;session-&gt;userdata('access_level') == FALSE)
      {
          redirect ('site/index');// to home page
      }
  
      //Stores the returned array in instance called "formdata" which will be passed to the view to be used in pulldown menu
      $page['formdata']=$this-&gt;submit_model-&gt;retrieve_values();
  
      //This loads the form 
      //Instance of "page" in array "page" specifies the file name of the page to load
      $page['page'] = 'submit';
      $this-&gt;load-&gt;view('template', $page );
  
      return;
  }
  </code></pre>
  
  <p>This is the part of the view which is causing the problem: I am using a foreach and then echoing the instances of the array into the option.</p>
  
  <pre><code>&lt;select&gt;
      &lt;?php foreach ($formdata as $row) { ?&gt;
          &lt;option value="&lt;?php echo $row-&gt;staff_id; ?&gt;"&gt;&lt;?php echo $row-&gt;name; ?&gt;&lt;/option&gt;
      &lt;?php } ?&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><code>printr()</code> of the variable <code>$formdata</code> shows that it contains these values:</p>
  
  <pre><code>Array (
      [0] =&gt; Array (
          [staff_id] =&gt; 1
          [name] =&gt; Cardiology Nurse
      )
      [1] =&gt; Array (
          [staff_id] =&gt; 2
          [name] =&gt; Radiology Nurse
      )
      [2] =&gt; Array (
          [staff_id] =&gt; 3
          [name] =&gt; Scrub Nurse
      )
      [3] =&gt; Array (
          [staff_id] =&gt; 4
          [name] =&gt; Circulating Nurse
      )
      [4] =&gt; Array (
          [staff_id] =&gt; 5
          [name] =&gt; Nurse
      )
      [5] =&gt; Array (
          [staff_id] =&gt; 6
          [name] =&gt; Training Nurse
      )
      [6] =&gt; Array (
          [staff_id] =&gt; 7
          [name] =&gt; Physiologist
      )
      [7] =&gt; Array (
          [staff_id] =&gt; 8
          [name] =&gt; Radiographer
      )
      [8] =&gt; Array (
          [staff_id] =&gt; 9
          [name] =&gt; Consultant
      )
      [9] =&gt; Array (
          [staff_id] =&gt; 10
          [name] =&gt; Radiologist
      )
      [10] =&gt; Array (
          [staff_id] =&gt; 11
          [name] =&gt; Cardiologist
      )
      [11] =&gt; Array (
          [staff_id] =&gt; 12
          [name] =&gt; Anaethestist
      )
      [12] =&gt; Array (
          [staff_id] =&gt; 13
          [name] =&gt; Non-medical Staff
      )
  )
  </code></pre>