/    Sign up×
Community /Pin to ProfileBookmark

automatically adding values in html tables

hi this is my first post here ?

i have php-ed some database data into very simple html tables, ordered by date, and wish to calculate (with javascript?!) subtotals for each day, e.g.

[FONT=”Courier New”]
date | number | subtotal
2007-08-01 | 10 | 10
2007-08-01 | 15 | 25
2007-08-01 | 2 | 27
2007-08-02 | 8 | 8
2007-08-02 | 9 | 17[/FONT]

can anyone help me start – i have no idea where to begin ?

many many thanks,
matt!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsSep 10.2007 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

function CalTotals(id){
var table=document.getElementById(id);
var rows=table.getElementsByTagName('TR');
var total=0;
for (var zxc0=0;zxc0<rows.length;zxc0++){
var cols=rows[zxc0].getElementsByTagName('TD');
if (cols[1].firstChild.nodeType==3&&parseInt(cols[1].firstChild.data)){
total+=parseInt(cols[1].firstChild.data);
cols[2].appendChild(document.createTextNode(total));
}
}
}

/*]]>*/
</script></head>

<body>
<table id="tst" cellpadding="0" cellspacing="0" border="1">
<tr>
<td width="100">Date</td>
<td width="100">Number</td>
<td width="100">Total</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>10</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>2</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>7</td>
<td>&nbsp;</td>
</tr>
</table>
<input type="button" name="" value="Cal Totals" onclick="CalTotals('tst')" /></body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@toicontienSep 11.2007 — I propose an alternate solution, using some properties inherent to tables:
/**
* @class window
*
* @function sumTableColumn
* Sums all the numbers in one column with the given header name.
*
* @param tNode (string or object, required)
* Id of a TABLE, TBODY, THEAD or TFOOT tag, or a node reference to one
* of those tags.
*
* @param headerName (string, required)
* The Id of a TH tag in the table to which certain cells are bound.
* This value should be found in the headers="" attribute of the cells.
*
* @param outputSumNode (string or object, optional)
* Id or node reference to an HTML tag that should display the sum.
*
* @return number
* The sum of the given column.
*/
function sumTableColumn(tNode, headerName, ouputSumNode) {
var rowsEnd = currentRow = sum = currentCell = cellsEnd = 0;
var row, cell;

if (typeof(tNode) === 'string') {
tNode = document.getElementById(tNode);
}
if (typeof(outputSumNode) === 'string') {
outputSumNode = document.getElementById(outputSumNode);
} else if (typeof(outputSumNode) !== 'object') {
outputSumNode = false;
}

rowsEnd = tNode.rows.length
for (currentRow; currentRow &lt; rowsEnd; currentRow++) {
row = tNode.rows[currentRow];
cellsEnd = row.cells.length;
for (currentCell = 0; currentCell &lt; cellsEnd; currentCell++) {
cell = row.cells[currentCell];
if (cell.headers.indexOf(headerName) &gt; -1) {
sum += parseFloat(cell.innerHTML);
}
}
}

if (outputSumNode) {
outputSumNode.innerHTML = sum;
}

return sum;
}

Here's an example page:
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- begin hiding
/**
* @class window
*
* @function sumTableColumn
* Sums all the numbers in one column with the given header name.
*
* @param tNode (string or object, required)
* Id of a TABLE, TBODY, THEAD or TFOOT tag, or a node reference to one
* of those tags.
*
* @param headerName (string, required)
* The Id of a TH tag in the table to which certain cells are bound.
* This value should be found in the headers="" attribute of the cells.
*
* @param outputSumNode (string or object, optional)
* Id or node reference to an HTML tag that should display the sum.
*
* @return number
* The sum of the given column.
*/
function sumTableColumn(tNode, headerName, ouputSumNode) {
var rowsEnd = currentRow = sum = currentCell = cellsEnd = 0;
var row, cell;

if (typeof(tNode) === 'string') {
tNode = document.getElementById(tNode);
}
if (typeof(outputSumNode) === 'string') {
outputSumNode = document.getElementById(outputSumNode);
} else if (typeof(outputSumNode) !== 'object') {
outputSumNode = false;
}

rowsEnd = tNode.rows.length
for (currentRow; currentRow < rowsEnd; currentRow++) {
row = tNode.rows[currentRow];
cellsEnd = row.cells.length;
for (currentCell = 0; currentCell < cellsEnd; currentCell++) {
cell = row.cells[currentCell];
if (cell.headers.indexOf(headerName) > -1) {
sum += parseFloat(cell.innerHTML);
}
}
}

if (outputSumNode) {
outputSumNode.innerHTML = sum;
}

return sum;
}


window.onload = function() {
alert(sumTableColumn('mytable','th_numbers'));
};
// end hiding -->
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" id="mytable">
<thead>
<tr>
<th id="th_letters">Letters</th>
<th id="th_numbers">Numbers</th>
</tr>
</thead>
<tbody>
<tr>
<td headers="th_letters">a</td>
<td headers="th_numbers">10</td>
</tr>
<tr>
<td headers="th_letters">b</td>
<td headers="th_numbers">3</td>
</tr>
<tr>
<td headers="th_letters">r</td>
<td headers="th_numbers">15</td>
</tr>
</tbody>
</table>
</body>
</html>[/code]

This function requires a well formed table utilizing semantic HTML.

And finally some references:

Table DOM: http://www.w3schools.com/htmldom/dom_obj_table.asp

Table Row DOM: http://www.w3schools.com/htmldom/dom_obj_tablerow.asp

Table Cell DOM: http://www.w3schools.com/htmldom/dom_obj_tabledata.asp
Copy linkTweet thisAlerts:
@toicontienSep 11.2007 — And to turn your sample data into a table:
[code=html]<table cellpadding="3" cellspacing="0" border="1" id="mytable">
<thead>
<tr>
<th id="th_date">Date</th>
<th id="th_number">Number</th>
<th id="th_subtotal">Subtotal</th>
</tr>
</thead>
<tbody>
<tr>
<td headers="th_date">2007-08-01</td>
<td headers="th_number">10</td>
<td headers="th_subtotal">10</td>
</tr>
<tr>
<td headers="th_date">2007-08-01</td>
<td headers="th_number">15</td>
<td headers="th_subtotal">25</td>
</tr>
<tr>
<td headers="th_date">2007-08-01</td>
<td headers="th_number">2</td>
<td headers="th_subtotal">27</td>
</tr>
<tr>
<td headers="th_date">2007-08-02</td>
<td headers="th_number">8</td>
<td headers="th_subtotal">8</td>
</tr>
<tr>
<td headers="th_date">2007-08-02</td>
<td headers="th_number">9</td>
<td headers="th_subtotal">17</td>
</tr>
</tbody>
</table>[/code]
Copy linkTweet thisAlerts:
@samanyoluSep 11.2007 —  <br/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;meta content="text/html; charset=UTF-8" http-equiv="content-type"&gt;
&lt;title&gt;&lt;/title&gt;


&lt;script type="text/javascript"&gt;
function calculate () {
var table = document.getElementById('tableid');
table.rows[1].cells[2].innerHTML = table.rows[1].cells[1].innerHTML;
for( var n = 2; n &lt; table.rows.length; n ++ ) {
if( table.rows[n].cells[0].innerHTML == table.rows[n-1].cells[0].innerHTML ) {
table.rows[n].cells[2].innerHTML = Number(table.rows[n-1].cells[2].innerHTML) + Number(table.rows[n].cells[1].innerHTML); }
else { table.rows[n].cells[2].innerHTML = table.rows[n].cells[1].innerHTML; }
}
}
&lt;/script&gt;
&lt;/head&gt;


&lt;body&gt;

&lt;input value="calculate" onclick="calculate()" type="button"&gt;
&lt;table id="tableid" style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2"&gt;

&lt;tbody&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;&lt;span style="font-weight: bold;"&gt;date&lt;/span&gt;&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;span style="font-weight: bold;"&gt;number&lt;/span&gt;&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;span style="font-weight: bold;"&gt;subtotal&lt;/span&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;2007-08-01&lt;/td&gt;

<i> </i> &lt;td&gt;10&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;2007-08-01&lt;/td&gt;

<i> </i> &lt;td&gt;15&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;2007-08-01&lt;/td&gt;

<i> </i> &lt;td&gt;2&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;2007-08-02&lt;/td&gt;

<i> </i> &lt;td&gt;8&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

<i> </i>&lt;tr&gt;

<i> </i> &lt;td&gt;2007-08-02&lt;/td&gt;

<i> </i> &lt;td&gt;9&lt;/td&gt;

<i> </i> &lt;td&gt;&lt;/td&gt;

<i> </i>&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;

&lt;br&gt;

&lt;/body&gt;
&lt;/html&gt;

Copy linkTweet thisAlerts:
@mattshortauthorSep 12.2007 — hi all

this stuff is perfect... but from first glance am i right in saying it does a cumulative total for the entire column - rather than a cumulative total for that day?

i.e. there could be many instances of a single date in the left column, for which i need a subtotal in the final column. e.g.

01/08 - 2 - 2

01/08 - 2 - 4

01/08 - 3 - 7

01/08 - 2 - 9

02/08 - 5 - 5

02/08 - 9 - 14

03/08 - 1 - 1

a grand total is not required. (also it may not be dates in the first column for other tables - so if it makes a difference(?) any code should look for matching 'strings' rather than matching 'dates')

is this even possible :rolleyes:

matt
×

Success!

Help @mattshort spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...