title: CodeIgniter: Passing database array data to a template file
body: <p>In my application I am trying to populate a sidebar menu list with rows from a database table. More specifically, I try to retrieve team name where the coach has the same user_id as the user that is logged in. I want to list the logged in coach's teams listed in a menu. </p>
  
  <p>In my Model, I am using Active Record to get the desired query results: </p>
  
  <pre><code>public function get_team()
  {
      $this-&gt;db-&gt;select('teamname')-&gt;from('teams')-&gt;where('coaches', $this-&gt;session-&gt;userdata('user_id'));
      $this-&gt;db-&gt;order_by('teamname', 'asc');
      $query = $this-&gt;db-&gt;get();
  
      if($query-&gt;num_rows() &gt; 0)
      {
          foreach($query-&gt;result_array() as $row)
          {
              $data[] = $row;
          }
          return $data;
      }   
  }
  </code></pre>
  
  <p>In the Controller, I am not sure how to go about in order to pass the array data to my view. In my index function, I do this:</p>
  
  <pre><code>public function index()
  {
      if (!$this-&gt;tank_auth-&gt;is_logged_in()) {
          redirect('/auth/login/');
      } else 
          {
              $data['title'] = 'Team';
              $data['main_content'] = 'team_v';
              $data['username'] = $this-&gt;tank_auth-&gt;get_username();
              $data['coach'] = $this-&gt;tank_auth-&gt;is_admin();
              $this-&gt;load-&gt;vars($data);
              $this-&gt;load-&gt;view('includes/template');
          }
  }
  </code></pre>
  
  <p>And in the relevant controller function, I try to send the data directly to the sidebar view, (which is included in the previously loaded template file):</p>
  
  <pre><code>public function get_team()
  {
      $data = array(
      'result' =&gt; $this-&gt;team_m-&gt;get_team());
  
      $this-&gt;load-&gt;view('includes/sidebar', $data);
  }
  </code></pre>
  
  <p>The view code performs a standard foreach and looks like this:
      <ul></p>
  
  <pre><code>&lt;li&gt;Teams&lt;/li&gt;
      &lt;ul&gt;
          &lt;?php /* if (empty($result)): */ ?&gt;
          &lt;li&gt;Create a team&lt;/li&gt;
          &lt;?php /* else: */ ?&gt;
          &lt;?php foreach ($result as $row): ?&gt;
          &lt;li&gt;&lt;?php echo $row['teamname'];?&gt;&lt;/li&gt;  
          &lt;?php endforeach; ?&gt;
          &lt;?php /* endif; */ ?&gt;
      &lt;/ul&gt;
  
  &lt;li&gt;Messages&lt;/li&gt;
  &lt;li&gt;My profile&lt;/li&gt;
  &lt;li&gt;&lt;a href="&lt;?php echo base_url('index.php/auth/logout'); ?&gt;"&gt;Logout&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p></ul></p>
  
  <p>Sending the $data array to the sidebar this way doesn't work. I have also tried to load all the views in the template manually and passing the data to the views, but to no avail. No matter how I pass it, it gives me the "Invalid argument supplied for foreach()" error, which AFAIK means that no data gets returned in the array. </p>
  
  <p>However, if I do this in the controller function:</p>
  
  <pre><code>public function get_team()
  {
      $data = array(
      'result' =&gt; $this-&gt;team_m-&gt;get_team());
  
      $this-&gt;load-&gt;view('get_team_v', $data);
  }
  </code></pre>
  
  <p>and make a get_team_v view with the exact same code in the sidebar view above, then the desired result is listed perfectly. I have also performed an affected_rows test and confirmed that the query part is good. What should I do to get the array data to be listed also in the templated sidebar view?</p>