I have 2 tables in a DB that I am joining with a PDO executes and I have it outputting in one form of table but I need it in a another format. Do I need some form of dynamic pivot?
The 2 tables are like this:
I am looking to get the following table from it:
The current query I am using to collate the data is:
$sql3 = $pdo->prepare('SELECT b.title, a.nameFROM poll_summary a INNER JOIN poll_answers b ON a.answers_id = b.id WHERE a.poll_id = ? ORDER BY title');
$sql3->execute([$_GET['id']]);
$testing = $sql3->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);
This is a sample array of what it gives:
Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )
I can get the table headers from the title column using:
<table border=1>
<thead>
<tr>
<?php
foreach($testing as $key => $val):
?>
<th><?php echo $key;?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
</tbody>
</table>
But I can't figure out how to get the name values into the correct columns. I tried using this code but just can't get it to work how I want it to.
foreach($testing as $key => $val) {
$arrlength = count($val);
for($x = 0; $x < $arrlength; $x++) {
//echo $key;
echo $key;
echo $val[$x]['name'];
}
}
Outputs:
Knight Dave Knight Simon Lieutenant Tom