title: CodeIgniter: Displaying data from two tables in a view - use one loop?
body: <p>I'd like some help please. 
  I have these two database tables, with relationship 1-n (one album has many images):</p>
  
  <pre><code>  albums            images
  ----------       -------------
  album_id            img_id
  title               album_id
  description         img_file
  </code></pre>
  
  <p>Now what I'm trying to do is to <strong>show all the albums with their images in my view</strong>, something like <a href="http://net.tutsplus.com/articles/news/free-website-template/" rel="nofollow">this</a>, see the 2nd screenshot (portfolio).</p>
  
  <p>This is what I have in my Controller:</p>
  
  <pre><code>// fetch all album data 
  $albums = $this-&gt;album_model-&gt;get();
  
  // fetch images for each album
  foreach ($albums as $album ) {
   $images = $this-&gt;albumimage_model-&gt;get_by('album_id', $album-&gt;album_id);
  }
  
  $data = array(
   'albums'  =&gt;  $albums, 
   'images'  =&gt;  $images, 
  );
  $this-&gt;load-&gt;view('album_listing', $data); 
  </code></pre>
  
  <p>If i loop through the album data in my view, I get this:</p>
  
  <pre><code>&lt;?php foreach($albums as $album): ?&gt;
  &lt;div class="album"&gt;
  &lt;p&gt;&lt;?php echo $album-&gt;title; ?&gt;&lt;/p&gt;
  &lt;p&gt;&lt;?php echo $album-&gt;description; ?&gt;&lt;/p&gt;
  &lt;div&gt;
  &lt;?php endforeach; ?&gt; 
  </code></pre>
  
  <p>So far so good, but now here comes the headache:
  <strong>I want to display the images that belong to each album</strong>, so basicle something like this:</p>
  
  <pre><code>&lt;?php foreach($albums as $album): ?&gt;
  &lt;div class="album"&gt;
      &lt;p&gt;&lt;?php echo $album-&gt;title; ?&gt;&lt;/p&gt;
      &lt;p&gt;&lt;?php echo $album-&gt;description; ?&gt;&lt;/p&gt;
  
      // show all images of the album
      &lt;div class="images"&gt;&lt;img src="uploads/&lt;php? echo $album-&gt;title;  ?&gt;.'/'.&lt;php? echo $image-&gt;image_file;  ?&gt; " /&gt;&lt;/div&gt;
  
  &lt;div&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>How can I accomplish that, according to the CodeIgniter practices???</p>
  
  <p>Some additional info: 
  1. I have also enable the CodeIgntier's Profiler and I've noticed that the running queries are incremented like this. Whouldn't this increase the memmory in case I have many albums (50 - 100 albums with images)??? </p>
  
  <ol>
  <li><p>I also tried to do this using just one query with JOIN, something like this:</p>
  
  <p>$albums = $this->db->select('albums.*')
    ->select('images.img_id, images.img_file')
    ->join('images', 'images.album_id = albums.album_id')
    ->order_by('albums.album_id')
    ->get('albums')
    ->result_array();</p></li>
  </ol>
  
  <p>The problem with this is when I loop through the data (lets say I have two albums in my database), I get the data twice on my page, like :</p>
  
  <pre><code>album1 - images of album 1
  album1 - images of album 1
  album2 - images of album 2
  album2 - images of album 2
  </code></pre>