简体   繁体   English

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

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

I make a table like this code: 我制作一个像这样的代码表:

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

Now i want display the sum of each rows and column of the html table. 现在,我想显示html表的每一行和每一列的总和。 For example I chose a matrix of: 5*5 so i have: 例如,我选择的矩阵为: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


I want display to the left of each row and in each column the appropriate sum, such as: 我想在每一行和每一列的左侧显示适当的总和,例如:

row(1) => 15, row(2) => 40, row(3) => 65 and so on.. column(1) => 55 , column(2) => 60 and so on.. row(1) => 15, row(2) => 40, row(3) => 65 ,依此类推。column column(1) => 55 , column(2) => 60 ,依此类推。
How to make this? 怎么做?

You can try something like this: 您可以尝试如下操作:

$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>';

You use $i and $j values that indicate at which column or row are you at in your iteration and sum all the values that belong to the same column or row in $column_sum and $row_sum respectively. 您可以使用$i$j指示在该列或行,你在你的迭代和总结属于同一列或行中的所有值的值$column_sum$row_sum分别。

EDIT : If you need to print the sum on the left (before you iterate trough that row) you will need to precalculate the sum. 编辑 :如果您需要在左边打印总和(在迭代该行之前),您将需要预先计算总和。 You can define two functions 您可以定义两个函数

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;
}

And then use them where you want. 然后在需要的地方使用它们。

One way to do would be like this, take the first value in each row and add itself to the total with +1,+2 and so on 一种方法是像这样,取每一行的第一个值,然后将自身加到+ 1,+ 2等上,依此类推

updated so it can be any column number 已更新,因此可以是任何列号

$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>
            ';
    }
}

This should work for you: 这应该为您工作:

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


?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM