title: Nested for each loop or SQL duplicating results
body: <p>HI all i have an issue echoing out my data.
  I have three tables, one for students, classes and student_classes.
  I've queried the db to joint the all three so i can see all students, whether they have enrolled in a class or not with their class. the sql is   </p>
  
  <pre><code>SELECT students.id, students.student_name, students.student_number,
  section_student.ss_id, section_student.section_number,sections.subject 
  FROM students 
  LEFT JOIN student_classes
  ON students.student_number=student_classes.student_number 
  LEFT JOIN classes 
  ON classes.class_number=student_classes.class_number 
  WHERE students.status=1
  </code></pre>
  
  <p>This produces a list of students with classes if they have them. for those students who have enrolled in more than one course there name shows twice in the list.</p>
  
  <p>When i print_r the data i get</p>
  
  <pre><code>Array
  (
  [0] =&gt; stdClass Object
      (
          [class_number] =&gt; 
          [student_name] =&gt; john smith
          [student_number] =&gt; 432100351
          [id] =&gt; 2
          [ss_id] =&gt; 
          [subject] =&gt; 
          [user_email] =&gt; john@email.com
          [created_at] =&gt; 0000-00-00 00:00:00
          [updated_at] =&gt; 0000-00-00 00:00:00
      )
  
  [1] =&gt; stdClass Object
      (
          [class_number] =&gt; 43223
          [student_name] =&gt; Paul jones
          [student_number] =&gt; 432100312
          [id] =&gt; 3
          [ss_id] =&gt; 40
          [subject] =&gt; Maths
          [user_email] =&gt; Paul@email.com
          [created_at] =&gt; 0000-00-00 00:00:00
          [updated_at] =&gt; 2012-11-24 08:33:23
      )
  
  [2] =&gt; stdClass Object
      (
          [class_number] =&gt; 21331
          [student_name] =&gt; Paul jones
          [student_number] =&gt; 432100312
          [id] =&gt; 3
          [ss_id] =&gt; 39
          [subject] =&gt; Physics
          [user_email] =&gt; Paul@email.com
          [created_at] =&gt; 0000-00-00 00:00:00
          [updated_at] =&gt; 2012-11-24 08:33:23
      )
  </code></pre>
  
  <p>When i do a for each in a table to show the students and their class I want to loop through the classes that a student has in one line. eg</p>
  
  <p>john ID:432100351 | N/A
  Paul ID:432100312 | 43223 = Maths , 21331 = Physics</p>
  
  <p>When i do my loops i get</p>
  
  <p>ID:432100351 john  | john@email.com | N/A <br/>
  ID:432100312 Paul  | paul@email.com | 43223 = Maths , 21331 = Physics<br/>
  ID:432100312 Paul  | paul@email.com | 43223 = Maths , 21331 = Physics<br/></p>
  
  <p>Im not sure if the problem is with my sql or my loop</p>
  
  <p>Here is the code</p>
  
  <pre><code>@foreach ($students as $student)
  &lt;tr&gt;
  &lt;td&gt; {{ $key++ }}&lt;/td&gt;
  &lt;td&gt; {{ HTML::link('students/edit/' .$student-&gt;id, $student-&gt;student_number) }} &lt;/td&gt;
  &lt;td&gt; {{ HTML::link('students/edit/' .$student-&gt;id, $student-&gt;student_name) }} &lt;/td&gt;
  &lt;td&gt; {{ $student-&gt;user_email }} &lt;/td&gt;
  &lt;td&gt;
  
  
      @if(!isset($student-&gt;ss_id)) 
  
          {{ HTML::link('add_student_section.php?student_id='.$student-&gt;id,'Add to section'); }}
  
      @else 
  
          @foreach($students as $section) 
  
           {{ HTML::link('edit_student_section.php?ss_id=', $section-&gt;section_number . " " . $section-&gt;subject . " ") ;}}     
  
          @endforeach
  
      @endif    
  
  &lt;/td&gt;
  &lt;td&gt; {{ date("d-M-Y H:i",strtotime($student-&gt;created_at )) }} &lt;/td&gt;
  &lt;td&gt; 
  @if($student-&gt;updated_at == $student-&gt;created_at)
  
  N/A
  
  @else
  
  {{ date("d-M-Y H:i",strtotime($student-&gt;updated_at )) }} 
  @endif
  
  &lt;/td&gt;
  &lt;td&gt; {{ HTML::link('students/delete/' .$student-&gt;id, 'Delete Student') }} &lt;/td&gt;
  &lt;/tr&gt;
  @endforeach
  </code></pre>
  
  <p>Any help would be appreciated. Thanks</p>