整晚都在头痛,但仍未找到解决方案。假设我有一个像这样的Mysql表结构:
ID name value year
1 Tom 15 2018
2 Tom 4 2019
3 Tom 6 2020
4 Kate 18 2018
5 Kate 20 2019
...and so on...
我希望通过PHP打印出类似下面的结果,其中年份应该是动态的,因为随着时间的推移而增加。请告诉我一个实现方法,谢谢。
year |2018 |2019|2020
----------------------
Tom | 15 | 4 | 6
----------------------
Kate | 18 | 20 | ---
----- and so on ---
我的代码:
<table>
<?php
$mysqli = new mysqli('localhost', 'root', '123', 'news');
$report = array();
$columnIndex = 0;
$query = $mysqli->query("SELECT name, value, year FROM Testab");
while ($results = $query->fetch_assoc()) {
foreach ($results as $tos => $toa) {
$report[$tos][$columnIndex] = $toa;
}
$columnIndex++;
}
foreach ($report as $tos => $results) { ?>
<tr>
<th><?php echo $tos; ?></th>
<?php foreach ($results as $toa) { ?>
<th><?php echo $toa; ?></th>
<?php } ?>
</tr>
<?php } ?>
</table>