title: Creating foreach loops using Code Igniter controller and view
body: <p>This is a situation I have found myself in a few times and I just want clear it up once and for all.</p>
  
  <p>Best just to show you what I need to do in some example code.</p>
  
  <p><strong>My Controller</strong></p>
  
  <pre><code>function my_controller()
  {
  
  $id = $this-&gt;uri-&gt;segment(3);
  
  $this-&gt;db-&gt;from('cue_sheets');
  $this-&gt;db-&gt;where('id', $id);
  $data['get_cue_sheets'] = $this-&gt;db-&gt;get();
  
  $this-&gt;db-&gt;from('clips');
  $this-&gt;db-&gt;where('sheet_id', ' CUE SHEET ID GOES IN HERE ??? ');
  $data['get_clips'] = $this-&gt;db-&gt;get();
  
  $this-&gt;load-&gt;view('show_sheets_and_clips', $data);
  
  }
  </code></pre>
  
  <p><strong>My View</strong></p>
  
  <pre><code>&lt;?php if($get_cue_sheets-&gt;result_array()) { ?&gt;
      &lt;?php foreach($get_cue_sheets-&gt;result_array() as $sheetRow): ?&gt;
          &lt;h1&gt;&lt;?php echo $sheetRow['sheet_name']; ?&gt;&lt;/h1&gt;
          &lt;br/&gt;
          &lt;?php if($get_clips-&gt;result_array()) { ?&gt;
              &lt;ul&gt;
                  &lt;?php foreach($get_clips-&gt;result_array() as $clipRow): ?&gt;
                      &lt;li&gt;&lt;?php echo $clipRow['clip_name']; ?&gt;&lt;/li&gt;
                  &lt;?php endforeach; ?&gt;
              &lt;/ul&gt;
          &lt;?php } else { echo 'No Clips Found'; } ?&gt;
      &lt;?php endforeach; ?&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>So basically I am trying to display a number of sheets and then within each of those sheets the clips that belong to that sheet.</p>
  
  <p>I hope this makes sense to someone out there.</p>
  
  <p>Thanks,</p>
  
  <p>Tim</p>