title: MYSQL and PHP group results by field data
body: <p>I am trying to create reports based on data from a log in the database that looks like:</p>
  
  <p>id | &nbsp; &nbsp;student &nbsp; &nbsp;| &nbsp; &nbsp;type &nbsp; &nbsp;    | &nbsp; &nbsp;marks<br/>
  1   &nbsp; &nbsp; &nbsp; 23494   &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;CAT1      &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;50<br/>
  2    &nbsp; &nbsp;&nbsp; &nbsp;23495     &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; CAT1      &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;20<br/>
  3    &nbsp; &nbsp;&nbsp; &nbsp;23494     &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;CAT2       &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;35<br/>
  4    &nbsp; &nbsp;&nbsp; &nbsp;23495     &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;MIDTERM     &nbsp;&nbsp;40<br/></p>
  
  <p>My select statement so far looks like this:</p>
  
  <pre><code>$res = @mysqli_query ($dbc, "SELECT id, student, type, GROUP_CONCAT(marks) AS mark, GROUP_CONCAT(type) AS types FROM log WHERE class = '1' AND term = '2' GROUP BY student DESC");
  
  // Fetch and print all the records....&lt;br&gt;
  while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
  
          echo '&lt;tr&gt;
              &lt;td align="left"&gt;'. $row['student'] .'&lt;/td&gt;';
  
          //$exams = split(",", $row['exams']); // 4,3,1,2
          $marks = split(",", $row['mark']); // 10,20,40,50
  
          foreach( $marks as $mark ) {
               echo '
                     &lt;td align="left"&gt;' . $mark . '&lt;/td&gt;
                  ';
          }
  
          echo '&lt;/tr&gt;';
  
  } //End LOOP
  
  //Then i end table
  </code></pre>
  
  <p>So far the data displays like so:</p>
  
  <p>STUDENT | CAT1 | CAT2 | MIDTERM <br/>
  23494 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;35<br/>
  23495 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;40<br/></p>
  
  <p>The problem is that the code is not arranging 'marks' according to 'type' (look at MIDTERM output for id 4 and corresponding display).</p>
  
  <p>Question:</p>
  
  <p>How do i display the results by student, followed by marks in the appropriate cell/group like so:?</p>
  
  <p>STUDENT | CAT1 | CAT2 | MIDTERM <br/>
  23494 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;35<br/>
  23495 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;40<br/></p>
  
  <p>Thanks in Advance Guys.</p>