如何在矩阵表 PHP 中创建总计行
Posted
技术标签:
【中文标题】如何在矩阵表 PHP 中创建总计行【英文标题】:How to create a Total row in a Matrix table PHP 【发布时间】:2018-05-21 09:08:04 【问题描述】:我正在尝试在矩阵表中为每列的总值创建一个新行。 预期输出:
Day | Jan | Feb | Mar | Apr | May | Jun | ... | Dec |
1 | 3.0 | 0.0 | 0.0 | 0.0 | 5.0 | 1.0 | ... | 1.0 |
2 | 6.0 | 0.0 | 0.0 | 0.0 | 6.0 | 1.0 | ... | 1.0 |
3 | 3.0 | 0.0 | 0.0 | 4.0 | 0.0 | 0.0 | ... | 0.0 |
4 | 4.0 | 1.0 | 0.0 | 3.0 | 0.0 | 0.0 | ... | 0.0 |
5 | 3.0 | 1.0 | 0.0 | 2.0 | 0.0 | 1.0 | ... | 0.0 |
6 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | ... | 0.0 |
7 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | ... | 2.0 |
8 | 0.0 | 0.0 | 0.0 | 2.0 | 1.0 | 0.0 | ... | 1.0 |
9 | 1.0 | 0.0 | 1.0 | 3.0 | 1.0 | 0.0 | ... | 0.0 |
10 | 4.0 | 0.0 | 1.0 | 3.0 | 0.0 | 0.0 | ... | 1.0 |
...
31 | 0.0 | 1.0 | 0.0 | 4.0 | 0.0 | 8.0 | ... | 0.0 |
Total |20.0 |12.0 |10.0 |11.0 |10.0 |30.0 | ... |10.0 |
代码如下:
while($row = odbc_fetch_array($rs))
$Station = $row['StationCode'];
$Year = $row['Year'];
$Day = $row['Day'];
$Month = $row['Month'];
$data[$Day][$Month] = $row['D_Rainfall'];
$montharray[] = $row['Month'];
<table class="table table bordered table-condensed">
<thead>
<tr>
<th>Day</th>
$montharray = array_unique($montharray);
foreach($montharray as $name)
echo '<th>' . $name . '</th>'; //Month
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach($data as $day=>$rainfall)
echo '<tr>';
echo '<th>'. $day .'</th>'; //Days
foreach($montharray as $name)
$D_rainfall = isset($rainfall[$name]) ? $rainfall[$name] : '-';
echo '<td>' . $D_rainfall . '</td>'; //Rainfall value per day/month
echo '</tr>';
echo '<tr>';
echo '<th>Total</th>';
echo '<td> Sum? </td>'; // What do I need to do here?
echo '</tr>';
echo '</tbody>';
echo '</table>';
我还需要在每一列中找到平均值,但我想如果我知道如何做到这一点,我可以处理平均行。
【问题讨论】:
您最好在 Ms Access 中创建一个单独的查询来计算这些总数(或使用联合查询将总数附加到查询的底部)。如果您想要更全面的答案,您需要提供您当前的表结构以及获取此数据的查询。SELECT StationCode, Year, Month, Day, D_Rainfall FROM DailyRainfall WHERE StationCode = 1 AND Year = 2016 ORDER BY Year DESC , Month ASC , Day ASC
这就是表格中的所有内容,我只是在某处阅读以避免在查询中使用*
。假设用户提示 stationcode = 1 和 2016 年。
【参考方案1】:
您可以使用以下代码:
while($row = odbc_fetch_array($rs))
$Station = $row['StationCode'];
$Year = $row['Year'];
$Day = $row['Day'];
$Month = $row['Month'];
$data[$Day][$Month] = $row['D_Rainfall'];
$montharray[] = $row['Month'];
$sumArray= [];
<table class="table table bordered table-condensed">
<thead>
<tr>
<th>Day</th>
$montharray = array_unique($montharray);
foreach($montharray as $name)
$sumArray[$name] = 0;
echo '<th>' . $name . '</th>'; //Month
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach($data as $day=>$rainfall)
echo '<tr>';
echo '<th>'. $day .'</th>'; //Days
foreach($montharray as $name)
$sumArray[$name] += isset($rainfall[$name]) ? $rainfall[$name] : 0
$D_rainfall = isset($rainfall[$name]) ? $rainfall[$name] : '-';
echo '<td>' . $D_rainfall . '</td>'; //Rainfall value per day/month
echo '</tr>';
echo '<tr>';
echo '<th>Total</th>';
foreach($montharray as $name)
echo '<td>'. $sumArray[$name].'</td>'; // What do I need to do here?
echo '</tr>';
echo '</tbody>';
echo '</table>';
【讨论】:
我想问如果我也想找到平均值和最小值/最大值怎么办?我试图在foreach
中使用数组来做到这一点,但它会显示全年的结果:$maxArray[] = isset($rainfall[$name]) ? $rainfall[$name] : 0;
和foreach($montharray as $name) echo '<td>'. max($maxArray) .'</td>';
现在这就是我找到最大值的方法以上是关于如何在矩阵表 PHP 中创建总计行的主要内容,如果未能解决你的问题,请参考以下文章