如何对 html 表的每一行和列的值求和


How to sum the value of each rows and colum of html table

我做了一个像这样的代码表:

$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等等。
怎么做?

你可以尝试这样的事情:

$rows = $_REQUEST['c'];
$columns = $_REQUEST['c2'];
$row_sum = array();
$column_sum = array();
for($i = 0; $i < $rows; $i++){
  echo '<tr>';
  for($j = 0; $j < $columns; $j++){
    $tmp = $i*$columns+($j+1);
    echo "<td><input type='text' name='' size='8' value='$tmp'/></td>";
    $row_sum[$i] += $tmp;
    $column_sum[$j] += $tmp; 
  }
  echo "<td>$row_sum[$i]</td></tr>";
}
echo '<tr>';
for($i = 0; $i < $columns; $i++){
  echo "<td>$column_sum[$i]</td>";
}
echo '</tr>';

您可以使用$i值和$j值来指示您在迭代中所在的列或行,并分别对属于同一列或行的所有值求和$column_sum$row_sum

编辑:如果需要打印左侧的总和(在迭代该行之前),则需要预先计算总和。您可以定义两个函数

function sum_row($row, $total_columns){
    $s = $total_columns * $row + 1;
    $e = $s + $total_columns-1;
    return ($e + 1 - $s) * ($e + $s) / 2;
}
function sum_column($column, $total_rows, $total_columns){
    $sum = 0;
    for($i = 0; $i < $total_rows; $i++){
        $sum += $i*$total_columns+$column+1;
    }
    return $sum;
}

然后在您想要的地方使用它们。

这应该适合您:

<?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>";

?>

一种方法是这样的,取每行中的第一个值,然后用 +1,+2 将其自身添加到总计中,依此类推

已更新,因此它可以是任何列号

$max = 80;
$column = 8;
echo '
    <table border="1">
        <tr>            
            <td>total</td>
        ';
for ($x = 1; $x <= $column; $x++) {
    echo '
            <td>' . $x . '</td>
        ';
}
echo '</tr>';
for ($x = 1; $x <= $max; $x++) {
    if ($x % $column == 1) {
        $total = $x;
        $value = $total;
        for ($y = 1; $y < $column; $y++) {
            $total = $total + $value + $y;
        }
        echo '
            <tr>
                <td>' . $total . '</td>
                <td>' . $x . '</td>
            ';
    } elseif ($x % $column == 0) {
        echo '
                <td>' . $x . '</td>
            </tr>
            ';
    } else {
        echo '
                <td>' . $x . '</td>
            ';
    }
}