This is my first post here, feeling good to be part of this community.
The case that brought me here is the following: An html table is printed with data from a database using php. I want to sum every table column and add it to the table. I have used innerHTML to do that, but what i get is every cell containing each column sum is added to the table vertically, one below the other and not horizontally. Another thing that needs to be mentioned is that the table column number may vary from query to query, since the column headers are the months of a specific period of time given by the user.
Here is the part of the code that intends to print the sums:
Code:
echo "<script language=\"javascript\" type=\"text/javascript\">
var tds = document.getElementById('sumtable').getElementsByTagName('td');
var t = document.createElement('sumtable');
var p = ".$k.";
document.getElementById('sumtable').innerHTML += '<th> Total </th>';
var c=0;
while(c<=p){
var sum = 0;
for(var i = 0; i < tds.length; i++) {
if(tds[i].className == 'class'+c) {
sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
}
}
document.getElementById('sumtable').innerHTML += '<td align=\"center\">' + sum + '</td>';
c++;
}
document.getElementById('sumtable').innerHTML += '</tr>';
</script> ";
I think that the problem is created by innerHTML, since in FF I have the aforementioned problem, while in IE, I can't see the sums at all. So, I would like to ask if there is another simpler way of doing this task.
The code above creates the last part of the table starting with "Total". You can see that even if there is no "</tr>" in the part of the while loop where the sums are printed in cells, the innerHTML is printing arbitrarily "</tr>" after each cell.
Bookmarks