我以下列格式从MYSQL表中提取了数据,
ABC 2016 567
ABC 2017 456
ABC 2018 987
防御2016 537
DEF 2017 452
EFG 2016 537
EFG 2017 452
EFG 2018 687
我需要通过Php在HTML表格中以以下格式显示它,
网站2016 2017 2018
ABC 567 456 987
国防部537452
EFG 537452687
任何帮助将不胜感激。
SQL查询:
SELECT sum(Volume) as Vol from rad_vol WHERE Modality like '%MR' group by Site, Year
Php代码:
<?php
$sql = "SELECT sum(Volume) as Vol from rad_vol WHERE Modality like '%MR' group by Site, Year ";
echo $sql;
$result = mysqli_query($conn, $sql);
$sql = "SELECT DISTINCT Year from rad_vol";
$res_year = mysqli_query($conn, $sql);
$sql = "SELECT DISTINCT Site from rad_vol";
$res_site = mysqli_query($conn, $sql);
?>
<div id="content">
<article>
<table>
<thead>
<tr>
<th>
SITE
</th>
<?php
if (mysqli_num_rows($res_year) > 0){
$yr_count=0;
while ($rows = mysqli_fetch_array($res_year))
{
$year = $rows["Year"];
echo '<th>' . $year . '</th>';
$yr_count++;
}
echo $yr_count;
}
?>
</tr>
<?php
while ($rows = mysqli_fetch_array($res_site))
{
echo '<tr><th>' . $rows["Site"] . '</th>';
$i = 0;
while($vols = mysqli_fetch_array($result))
{
echo '<td>' . $vols["Vol"] . '</td>';
$i++;
if ($i % 4 == 0){
echo '</tr>';
break;
}
}
}
?>
</thead>
</table>
答案 0 :(得分:-1)
距离您还不到一百万英里……
如果将其最多映射到几个数组并使用它们,则可以用一个查询来完成。
SQL:
SELECT
v.Site,
v.Year,
SUM(Volume) AS Vol
FROM rad_vol AS v
WHERE v.Modality LIKE '%MR'
GROUP BY v.Site, v.Year
这应该给您这样的记录集:
Site Year Vol
ABC 2016 567
ABC 2017 456
ABC 2018 987
DEF 2016 537
DEF 2017 452
EFG 2016 537
EFG 2017 452
EFG 2018 687
然后您可以遍历该记录集并将其推入两个数组中:
$a = []; // this will be your data
$b = []; // this will be a list of years
if($response = mysqli_query($db, $sql)) {
while($row = mysqli_fetch_assoc($response)) {
// set the site to a var as we're going to use it
// a couple of times
$site = $row['Site'];
// push the site onto the array as an index
if(!array_key_exists($site, $a)) $a[$site] = [];
// add the year and volumne to the site
$a[$site][$row['Year']] = $row['Vol'];
// add the year to the list if it's not already in
if(!in_array($row['Year'], $b)) $b[] = $row['Year'];
}
}
$a
将保存您的重组数据,而$b
只是年份列表。 $a
应该看起来像这样:
Array
(
[ABC] => Array
(
[2016] => 567
[2017] => 456
[2018] => 987
)
[DEF] => Array
(
[2016] => 537
[2017] => 452
)
[EFG] => Array
(
[2016] => 537
[2017] => 452
[2018] => 687
)
)
现在,数据的结构更像您想如何使用它(如果倾斜头部并斜视)。现在,您只需要遍历输出内容即可:
<div id="content">
<article>
<table>
<thead>
<tr>
<th>Site</th>
<!-- loop through the years -->
<?php foreach($b as $y): ?>
<th><?= $y; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<!-- loop through the sites -->
<?php foreach($a as $siteName => $siteData): ?>
<tr>
<!-- echo out the site name -->
<td><?= $siteName; ?></td>
<!--
loop through the years again so that the columns
are generated in the same way as they were for
the <thead> row
-->
<?php foreach($b as $y): ?>
<!--
check to see if *this* site has data for the current
year in the loop - if so echo that out in a <td>
otherwise just echo an empty <td>
-->
<td><?= array_key_exists($y, $siteData) ? $siteData[$y] : " "; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</article>
</div>
整个套件和kaboodle
这只是完整脚本的转储,就像在我的开发箱中一样,其中已删除样式和注释。
<?php
header('Content-Type: text/html;charset=utf-8');
$db = mysqli_connect('127.0.0.1', '******', '******', 'test');
$sql = <<<SQL
SELECT
v.Site,
v.Year,
SUM(Volume) AS Vol
FROM rad_vol AS v
WHERE v.Modality LIKE '%MR'
GROUP BY v.Site, v.Year
SQL;
$a = [];
$b = [];
if($response = mysqli_query($db, $sql)) {
while($row = mysqli_fetch_assoc($response)) {
$site = $row['Site'];
if(!array_key_exists($site, $a)) $a[$site] = [];
$a[$site][$row['Year']] = $row['Vol'];
if(!in_array($row['Year'], $b)) $b[] = $row['Year'];
}
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<div id="content">
<article>
<table>
<thead>
<tr>
<th>Site</th>
<?php foreach($b as $y): ?>
<th><?= $y; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($a as $siteName => $siteData): ?>
<tr>
<td><?= $siteName; ?></td>
<?php foreach($b as $y): ?>
<td><?= array_key_exists($y, $siteData) ? $siteData[$y] : " "; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</article>
</div>
</body>
</html>