2015-01-11 62 views
0

的科拉姆我做一個表像這樣的代碼:如何總結各行的價值和HTML表格

$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等等。
如何做到這一點?做

+0

我試過用一些foreach,但是,不幸的是似乎沒有這個算法的正確解決方案 – NonSonoIo

+0

而你想顯示左邊的每一行和列的總和? – Rizier123

+0

是的,這將是很好的,但只是我工作的總和 – NonSonoIo

回答

0

這應該爲你工作:

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


?> 
0

一種方法會是這樣,採取在每一行中的第一個值和其自身添加到總與+ 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> 
      '; 
    } 
} 
+0

如果列數發生變化怎麼辦? – pajaja

+0

這個只計算行值? – NonSonoIo

+0

我更新了它,以便您可以取任何列號 – Som1

1

你可以嘗試這樣的事情:

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

然後在你想要的地方使用它們。