title: Codeigniter: Generating student result sheet from three tables
body: <p>I have the following tables in my database. The tables <code>midterm</code> and <code>finalterm</code> contain marks of students.</p>
  
  <p><img src="https://i.stack.imgur.com/NOd3z.jpg" alt="enter image description here"></p>
  
  <p>Now I want to display a marks sheet of all the students in single page like following:</p>
  
  <p><img src="https://i.stack.imgur.com/V7SdZ.png" alt="enter image description here"></p>
  
  <p>I know how to query in single table but I am facing problem in this case as there are three tables.</p>
  
  <p>I am using Codeigniter, would you please kindly help me with what to code in my model and view file?</p>
  
  <p>Thanks in advance :)</p>
  
  <p>This is my Controller:</p>
  
  <pre><code>         $this-&gt;load-&gt;model('Mod_student');
       $data['records1']= $this-&gt;Mod_student-&gt;check1();
           $this-&gt;load-&gt;view('view_student',$data);
  </code></pre>
  
  <h1>Updated part</h1>
  
  <p>This is my view:</p>
  
  <pre><code>    &lt;?php if(count($records1) &gt; 0) { ?&gt;
  
        &lt;?php foreach ($records1 as $row){ ?&gt;
  
  
  
     &lt;table&gt;
     &lt;tr&gt; &lt;td&gt;&lt;?php echo $row['StudentName']; ?&gt;&lt;/td&gt;&lt;/tr&gt;
  
       &lt;tr&gt;
  
        &lt;td&gt;&lt;?php $midDate   =   explode(',',$row['MidDate']);
       foreach($midDate as $md)
           {
           echo $md;
           echo '&lt;br /&gt;'  ;
           }
      ?&gt;
           &lt;/td&gt; 
  
         &lt;td&gt;&lt;?php $midDate   =   explode(',',$row['MidSubject']);
       foreach($midDate as $md)
           {
           echo $md;
           echo '&lt;br /&gt;'  ;
           }
      ?&gt;
        &lt;/td&gt; 
  
         &lt;td&gt;&lt;?php $midDate   =   explode(',',$row['MidMarks']);
       foreach($midDate as $md)
           {
           echo $md;
           echo '&lt;br /&gt;'  ;
           }
      ?&gt;
         &lt;/td&gt; 
  
  
  
         &lt;/tr&gt;
  
  
  
  
  
       &lt;/table&gt;
  
  
  
  
  &lt;?php  } ?&gt;
  
  &lt;?php } else { echo "No Record Found";} ?&gt;
  </code></pre>
  
  <p>I have changed my view file and now it is working fine :) But there are some problems that I am facing. If I run the above script I get the following output</p>
  
  <p><img src="https://i.stack.imgur.com/sXpgh.jpg" alt="enter image description here"></p>
  
  <p>If you notice my table <code>midterm</code>, you would see that the student whose <code>id</code> is 3 has got the same marks in both English and Maths. But in my output it displays mark for only one subject in the middle. Is it happening because you used <code>distinct</code> in your query? I have tried removing the distinct but then it does not show exact data.</p>
  
  <p>Would you please kindly tell me how to fix the problem?</p>
  
  <p>And thanks again for your help.:) </p>