请有人帮我用php
来总结一下
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<th scope="row">month</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
</tr>
<tr>
<th colspan="9">2011</th>
</tr>
<tr>
<td>January</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>February</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>November</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>December</td>
<td>4</td>
<td>4</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<th>Year Total</th>
<th>5</th>
<th>6</th>
<th>0</th>
<th>0</th>
<th>1</th>
<th>1</th>
<th>0</th>
</tr>
<tr>
<th>Year % of grand Total</th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th colspan="2"> </th>
</tr>
<tr>
<th colspan="9"><blockquote>
<blockquote>
<p>2012</p>
</blockquote>
</blockquote></th>
</tr>
<tr>
<td>January</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>2</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>February</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>November</td>
<td>25</td>
<td>20</td>
<td>1</td>
<td>4</td>
<td>4</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>December</td>
<td>26</td>
<td>6</td>
<td>3</td>
<td>1</td>
<td>1</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<th>Year Total</th>
<th>52</th>
<th>27</th>
<th>4</th>
<th>5</th>
<th>7</th>
<th>6</th>
<th>0</th>
</tr>
<tr>
<th>Year % of grand Total</th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th colspan="2"> </th>
</tr>
</tbody>
</table>
<p> </p>
我想像我为这个示例图所做的那样对上面的表行求和,但是我失败了。查询循环并汇总所有行。请帮助我解决这个问题
<?php
$db=mysql_connect("localhost","root","") or
die ("Unable to connect to the database,check parameter connection");
mysql_select_db('dbname',$db) or die (mysql_error($db));
$csv_output = '';
?>
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<th scope="row">month</th>
<th>North West</th>
<th>North Central</th>
<th>North East</th>
<th>South South</th>
<th> South East</th>
<th>South West</th>
<th>Foreigners</th>
</tr>
<tr>
<?php
$startdate = 2011;
$enddate = date("Y");
$years = range ($startdate,$enddate);
foreach($years as $year)
{
echo '<tr>
<th colspan="9">'.$year .'</th>
</tr>
';
$month = strtotime($year.'-01-01');
$end = strtotime($year.'-12-01');
while($month <= $end)
{
?>
<tr>
<td><?php echo date('F', $month); ?></td>
<td>
<?php
$num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'North West' AND Effective like '%".date('Y-m', $month)."%'"); echo $sun[] = mysql_num_rows($num); $csv_output .= mysql_num_rows($num);?> </td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'North Central' AND Effective like '%".date('Y-m', $month)."%'"); echo $sunb[] =mysql_num_rows($num); $csv_output .= mysql_num_rows($num). ", ";;?>
</td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'North East' AND Effective like '%".date('Y-m', $month)."%'"); echo $sunc[] = mysql_num_rows($num); $csv_output .= mysql_num_rows($num). ", ";;?>
</td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'South South' AND Effective like '%".date('Y-m', $month)."%'"); echo $sund[] =mysql_num_rows($num); $csv_output .= mysql_num_rows($num). ", ";;?>
</td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'South West' AND Effective like '%".date('Y-m', $month)."%'"); echo $sune[] =mysql_num_rows($num); $csv_output .= mysql_num_rows($num). ", ";;?>
</td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'South East' AND Effective like '%".date('Y-m', $month)."%'"); echo $sunf[] =mysql_num_rows($num); $csv_output .= mysql_num_rows($num). ", ";;?>
</td>
<td> <?php $num = mysql_query("SELECT * FROM table_name WHERE PolZone = 'Foreigners' AND Effective like '%".date('Y-m', $month)."%'"); echo $sung[] =mysql_num_rows($num); $csv_output .= mysql_num_rows($num). "\n";;?>
</td>
<?php $month = strtotime("+1 month", $month); ?>
</tr>
<?php } ?>
<tr>
<td> <?php echo 'total'; ?>
<td> <?php
echo array_sum($sun); ?>
<td><?php
echo array_sum($sunb); ?>
<td><?php
echo array_sum($sunc); ?>
<td><?php
echo array_sum($sund); ?>
<td><?php
echo array_sum($sune); ?>
<td><?php
echo array_sum($sunf); ?>
<td><?php
echo array_sum($sung); ?></td>
</tr>
<?php } ?>
</table>
答案 0 :(得分:0)
您可以在关联数组中格式化结果,并循环显示预期的表格视图。
告诉你一个非常古老的学校方式......
<!--
<?php
$array = array(
'2011' => array(
'January' => array(0, 0, 1, 0, 3, 0, 1),
'February' => array(3, 5, 0, 2, 1, 0, 0),
'March' => array(0, 3, 0, 3, 0, 1, 2),
),
'2012' => array(
'January' => array(3, 5, 0, 2, 1, 0, 0),
'February' => array(0, 3, 0, 3, 0, 1, 2),
'March' => array(0, 0, 1, 0, 3, 0, 1),
)
);
?>
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<th scope="row">month</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
</tr>
<?php
foreach ($array as $year => $months) {
?>
<tr>
<th colspan="9"><?php echo $year; ?></th>
</tr>
<?php
$sum1 = 0;
$sum2 = 0;
$sum3 = 0;
$sum4 = 0;
$sum5 = 0;
$sum6 = 0;
$sum7 = 0;
foreach ($months as $month => $values) {
?>
<tr>
<td><?php echo $month; ?></td>
<td><?php echo $values[0]; ?></td>
<td><?php echo $values[1]; ?></td>
<td><?php echo $values[2]; ?></td>
<td><?php echo $values[3]; ?></td>
<td><?php echo $values[4]; ?></td>
<td><?php echo $values[5]; ?></td>
<td><?php echo $values[6]; ?></td>
</tr>
<?php
$sum1 = $sum1 + $values[0];
$sum2 = $sum2 + $values[1];
$sum3 = $sum3 + $values[2];
$sum4 = $sum4 + $values[3];
$sum5 = $sum5 + $values[4];
$sum6 = $sum6 + $values[5];
$sum7 = $sum7 + $values[6];
}
?>
<tr>
<th>Year Total</th>
<th><?php echo $sum1; ?></th>
<th><?php echo $sum2; ?></th>
<th><?php echo $sum3; ?></th>
<th><?php echo $sum4; ?></th>
<th><?php echo $sum5; ?></th>
<th><?php echo $sum6; ?></th>
<th><?php echo $sum7; ?></th>
</tr>
<tr>
<th>Year % of grand Total</th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th colspan="2"> </th>
</tr>
<?php
}
?>
</tbody>
</table>