php - Print an HTML table of array values and perform basic calculations -
not sure if possible or if approaching right way have been searching around couple of hours no luck solution. found couple of examples thought work none of them have worked in php 5.3. appreciated.
i want print table of array values (with calculations)...
my data...
$data1 = array('value1'=>'5','value2'=>'7','value3'=>'1'); $data1_sum = array_sum($data1); $data2 = array('value1'=>'4','value2'=>'1','value3'=>'3'); $data2_sum = array_sum($data2); i print out in html table below , add values each array...
<table> <tr> <td>5</td> <td>7</td> <td>1</td> <td>13</td><!-- sum of $data1 values --> </tr> <tr> <td>4</td> <td>1</td> <td>3</td> <td>8</td><!-- sum of $data2 values --> </tr> <tr> <td>9</td><!-- sum of value1 --> <td>8</td><!-- sum of value2 --> <td>4</td><!-- sum of value3 --> <td>21</td><!-- sum of $data1_sum + $data2_sum values --> </tr> </table>
define function this:
function printrow($arr){ echo "<tr>"; foreach($arr $v){ echo "<td>",$v,"</td>"; } echo "<td>", array_sum($arr), "</td>"; echo "</tr>"; } to use
echo '<table>'; printrow($data1); printrow($data2); echo '</table>';
Comments
Post a Comment