动态构建<th>和<tr>

The amount of columns in a table is based upon the amount of data in a table. The amount of rows in a table is based on the amount of rows in a different table. An example of these tables are below

environments        patches
  id | name           id |    name
 ____|______         ____|____________
   1 | dev             1 | firstPatch
   2 | test            2 | secondPatch
   3 | prod            3 | thirdPatch

The end result of what I'm looking for would be the following <table>

 _________________________________
| Patch Name  | dev | test | prod |
|_____________|_____|______|______|
| thirdPatch  |     |      |      |
| secondPatch |  x  |      |      |
|_firstPatch__|_____|______|______|

I understand how I could use a while() loop to build the table headers. And I understand how I could use a while() to build the rows.

echo "<tr>";
while ($env = $listEnvs->fetch_assoc()){
    echo "<th>".$env['name']."</th>"
}
echo "</tr>";

--AND--

while ($patch = $listPatches->fetch_assoc()){
    echo "<tr><td>".$patch['name']."</td></tr>";
}

What I am struggling with is that the cells for each patch/environment pair are going to have data that is pulled from several other tables. So knowing the patch.id as well as the environment.id is important. Example the cell with the x is patch.id = 2, environment.id = 1 When constructing the table, how can I make sure each cell has this necessary information?

Try to create an array like $content[$patch.id][$environment.id].

Then you can add echo($content[$patch.id][$environment.id]) to your while of the $patch. You walk through the patches, so $patch.id is known. For $environment.id, you should make an array with all environment.id's and walk through it with $environment.id[i], where $i should be increased with each new column (and set to 0 when a new row is started).

In (bad) pseudo-code:

walkthrough environments {

    save array with environments
    echo table headers

}

walk through patches {

    echo table 'row headers'

    walk through environment array {

        echo content[patch.id][environment.id]

    }

}