With PHP I generate something like this:
<form>
<table>
<tr>
<td>Total:</td>
<td><input type="text" id="total" disabled value="0"> </td>
</tr>
<tr>
<td>Field 1:</td>
<td><input type="text" id="field1" value="1"> </td>
</tr>
<tr>
<td>Field 2:</td>
<td><input type="text" id="field2" value="2"> </td>
</tr>
</table>
Now the first input ("total") should contain the sum of all the "field" inputs. Summing them in a PHP variable is not a problem. But once I am down at the end of the table, can I change the value of "total"?
Workarounds I can think of are
- placing the total at the end; but then my page would not look the way I desire.
- do the loop through the data twice, first to do the sum, then to write the HTML. But that looks inefficient.
- storing the sum in a hidden input and writing it later with javascript.
So, again, can I still change the value of "total" with PHP after having generated the entire table?
$output = 'new table row' . $output;// Option: Assemble data in output variable in the right order, and use a placeholder for the total, that can later be replaced using sprintf, str_replace or ...