title: How to display grouped data in separate tables with a PHP loop?
body: <p>I am new to phpmysqli. Here is what I have and what am trying to achieve: I will update this based on the recommendations; 
  <a href="https://i.stack.imgur.com/hJryA.png" rel="nofollow noreferrer">Database sample data</a></p>
  
  <p>I want to display the data on one page with separate tables for each student based on their sid. This is what I have tried so far;</p>
  
  <pre><code>&lt;?php
  include_once 'dbcon.php';
  
  $results = $MySQLiconn-&gt;query('SELECT * FROM activitybook');
  
  $students = [];
  
  foreach ( $results-&gt;fetch_array() as $activity ) {
      $students[$activity['sid']][] = $activity;
  }
  
  foreach($students as $sid=&gt;$activities) {
      foreach($activities as $activity) {
           echo
                      "&lt;table&gt;&lt;tr&gt;
                          &lt;th&gt;SID&lt;/th&gt;
                          &lt;th&gt;Date&lt;/th&gt;
                          &lt;th&gt;FName&lt;/th&gt;
                          &lt;th&gt;LName&lt;/th&gt;
                          &lt;th&gt;activity&lt;/th&gt;
                          &lt;th&gt;time&lt;/th&gt;
                          &lt;th&gt;score&lt;/th&gt;
                          &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;" . $sid . "&lt;/td&gt;
                      &lt;td&gt;" . $activity['fname'] . "&lt;/td&gt;
                      &lt;td&gt;" . $activity['lname'] . "&lt;/td&gt;
                      &lt;td&gt;" . $activity['activity'] .  "&lt;/td&gt;
                      &lt;td&gt;" . $activity['atime'] .  "&lt;/td&gt;
                      &lt;td&gt;" . $activity['ascore'] .  "&lt;/td&gt;
                  &lt;/tr&gt;&lt;/table&gt;";
      }
  }
  ?&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BR3rH.png" rel="nofollow noreferrer">This is what I get</a></p>
  
  <p>What am trying to achieve is separate tables for each <code>sid</code>.
  <a href="https://i.stack.imgur.com/bGz1L.png" rel="nofollow noreferrer">This is the sample of what I want to archive</a></p>