While I would tend to agree in most respects with laserlight about the comma-separated fields, I can see where for the sake of the operator UX, without a more complex GUI, it might be easier for the operator to use the comma-separated fields (therefore revealing a common programming issue: namely that we must write complex systems in order for them to appear simple for the end-user).
I can make your data work with this code. I note that the output "ID" appears to be arbitrary, so I created $x
. You can create an array to act as a "lookup table" in order to get the names substituted into the "Selected Tenses" column.
$conn = mysqli_connect("localhost", "root", "", "test");
// get tense_names into an array
$tense_names = [];
$sql = "select * from tenses;";
$q = $conn->query($sql); // alternate, "OO" way for querying DB
while ($row = $q->fetch_assoc()) {
$tense_names[$row['pktenseid']] = $row['tense_name'];
}
// get the data
$sql = "SELECT t.tense_name, c.tenid, c.cat_name FROM tenses t, tensecategory c WHERE t.pktenseid=c.tenid";
$getcat = mysqli_query($conn, $sql); // this is also "okay"
$tenseinfo = [];
while ($row = mysqli_fetch_assoc($getcat)) {
if (!isset($tenseinfo[$row['tenid']])) {
$tenseinfo[$row['tenid']] = ['cat_name' => $row['cat_name'], 'tenses' => [$row['tense_name']]];
} else {
$tenseinfo[$row['tenid']]['tenses'][] = $row['tense_name'];
}
}
//echo header row
echo '<table><tr><th>ID</th><th>Category Name</th><th>Selected Tenses</th></tr>\n';
$x = 1; //arbitrary counter?
// main loop, creation of the table data
foreach ($tenseinfo as $catid => $data) {
echo '<tr><td>' . $x . '</td>';
echo '<td>' . $data['cat_name'] . '</td>';
$tense_nums = explode(',', $catid); // separate your comma-values
$string = '';
// look-up the tenses in our $tense_names array and concat them to our string
foreach ($tense_nums as $lookup) {
$string .= $tense_names[$lookup] . ", ";
}
echo '<td>' . rtrim($string, ",") . '</td></tr>';
$x++;
}
echo '</table>';