我做一个像这样的代码表:
$righe = $_REQUEST['c'];
$colonne = $_REQUEST['c2'];
$somma = 0;
echo"<table width='200' border='1'>";
for($a=1;$a<=$righe;$a++)
{
echo"<tr bgcolor='#0099FF'>";
for($b=1;$b<=$colonne;$b++)
{
$somma++;
$totale = $somma;
echo"<td><input type='text' name='' size='8' value =" .$totale. " /></td>";
}
echo"</tr>";
}
echo"</table>";
现在,我想显示html表的每一行和每一列的总和。例如,我选择的矩阵为:5 * 5,所以我有:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
我想在每一行和每一列的左侧显示适当的总和,例如:
row(1) => 15, row(2) => 40, row(3) => 65
等等..column(1) => 55 , column(2) => 60
等等..
如何做到这一点?
这应该为您工作:
<?php
$row = $_REQUEST['c'];
$column = $_REQUEST['c2'];
echo"<table width='200' border='1'>";
//Header
echo "<tr bgcolor='#0099FF'><td><input type='text' name='' size='8' value ='Row Sum:' /></td>";
foreach(range(1, $column) as $innerValue)
echo "<td><input type='text' name='' size='8' value =' ' /></td>";
echo "</tr>";
foreach(range(1, $row + 1) as $value) {
//Column Sum
if($value == $row + 1) {
echo "<tr bgcolor='#0099FF'><td><input type='text' name='' size='8' value ='Column Sum:' /></td>";
foreach(range(1, $column) as $innerValue)
echo "<td><input type='text' name='' size='8' value =" . array_sum(range($innerValue, ($innerValue+(($row-1)*$column)), $column)) . " /></td>";
echo "</tr>";
break;
}
echo"<tr bgcolor='#0099FF'>";
//Row Sum
echo "<td><input type='text' name='' size='8' value =" . (array_sum(range(1+(($value-1)*$column), $column+(($value-1)*$column)))) . " /></td>";
//Values
foreach(range(1, $column) as $innerValue)
echo"<td><input type='text' name='' size='8' value =" . ($innerValue+(($value-1)*$column)) . " /></td>";
echo"</tr>";
}
echo"</table>";
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句