title: PHP for html table with dynamic rows and columns where row data relies on column heading
body: <p>I have created an assessment system that grades each pupil on each lesson.  I have a form that creates a table with the following headings:</p>
  
  <ul>
  <li>Pupil name</li>
  <li>Pupil ID</li>
  <li>Lesson title</li>
  <li>Lesson ID</li>
  <li>Lesson grade</li>
  </ul>
  
  <p>To view the data I am trying to create a table that works a bit like a spreadsheet.<br>
  Where each row takes the pupil ID and searches in the database for the grade i.e.</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM table WHERE pupilid = "rowid" AND lessonid = "columnheading"
  </code></pre>
  
  <p>I can create the column and row headings using PHP with while loops, but I can't figure out how to make each cell link between the column heading and pupil ID.</p>
  
  <p>The only way I have managed to create this is with floating divs.<br>
  It works but it is very hard to style and the names and grades are in different grids so it is difficult to sort.<br>
  I would really appreciate any help / links to ways to do this.<br>
  Each week new pupils could be added and each lesson column will be added to the spreadsheet.</p>
  
  <p>The table should sort of look like the one below:</p>
  
  <pre class="lang-none prettyprint-override"><code>+-------+----+----+--- +
  | Pupil | L1 | L2 | L3 |
  +-------+----+----+----+
  | John  | B  | C+ | D  |
  +-------+----+----+----+
  | Sarah | B  | A  | F  |
  +-------+----+----+----+
  |  Jim  | D  | A  | B  |
  +-------+----+----+----+
  </code></pre>
  
  <p>I can create the column/row headings using the code below. I would really appreciate some help getting the code for the grade bit.</p>
  
  <pre><code>&lt;table&gt;
  &lt;tr&gt;
      &lt;th&gt;Pupil name&lt;/th&gt;
      &lt;?php
          $selectlesson=$connect-&gt;query("SELECT DISTINCT lessonid FROM `grades` ");
    while($rowslesson=$selectlesson-&gt;fetch_array())
      {
      ?&gt;
      &lt;th&gt;&lt;?php echo $rowslesson['lessonid']; ?&gt;&lt;/th&gt;
      &lt;?php
      }
      ?&gt;
  
  &lt;/tr&gt;
  &lt;?php
          $selectpupil=$connect-&gt;query("SELECT DISTINCT pupilid FROM `grades` ");
    while($rowspupil=$selectpupil-&gt;fetch_array())
      {
      ?&gt;
      &lt;tr&gt;
      &lt;td&gt;&lt;?php echo $rowspupil['pupilid']; ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php
      }
      ?&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thanks in advance for your help.</p>