Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 73935

Array into html table using PHP MySQL

$
0
0

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:

Tables in database

I am looking to get the following table from it:

enter image description here

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

Viewing all articles
Browse latest Browse all 73935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>