PDA

View Full Version : counting



marringi
04-02-2008, 10:32 PM
I'm running a database script and I am going to output a part of the database in a template page with a table of three columns and an unknown amount of rows. In every row the same word will appear in one column. What I would like to do is count the amount of times the word appears in every column and display it in a added bottom row. Here is as example:


Col1............Col2............Col3
Book............CD.............Video
Video............Book..........Magazine
Book...........Video...........CD
Cd...............Video..........Book
Magazine......Book..........CD
CD...............Book..........Video
2..................3.................1


Is this something that would be hard to do?
Does anyone know of a way that I can perform this?

thanks

Nile
04-03-2008, 02:47 AM
Hmmm, is this useful at all?:
http://www.tizag.com/javascriptT/javascript-string-indexOf.php
Hope this helps,
Nile

codeexploiter
04-03-2008, 03:56 AM
Here is a demo page.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">

</style>
<script type="text/javascript">
var rowCount = function(countWord){
/*I assume that the first row of the table will hold the column names and I've designed the script based on that if it is not like that it will differ somewhat*/
var rows = document.getElementById('tbl').getElementsByTagName('tr');
var cols = rows[1].getElementsByTagName('td').length;
var result = new Array(cols);

for(var m = 0; m < cols; m++){
result[m] = 0;
}

//Counting the total number mentioned word
for(var i = 1; i < rows.length; i++){
var tds = rows[i].getElementsByTagName('td');
for(var j = 0; j < tds.length; j++){
if(tds[j].childNodes.length == 1 && tds[j].childNodes[0].nodeValue == countWord)
result[j]++;
}
}
//Appending the result value in the last row's columns
var td = rows[rows.length - 1].getElementsByTagName('td');
for(var k = 0; k < td.length; k++){
td[k].appendChild(document.createTextNode(result[k]));
}
}
window.onload = function(){
rowCount('Book'); //Pass the word which you want to count
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1" width="300" id="tbl">
<tr>
<th width="100">First Field</th>
<th width="100">Second Field</th>
<th width="100">Third Field</th>
</tr>

<tr>
<td width="100">Book</td>
<td width="100">Mate</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Video</td>
<td width="100">Image</td>
</tr>

<tr>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Image</td>
</tr>

<tr>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</body>
</html>

Please note that from the JS point of view at the moment I am not checking whether the table, tr and td elements exists or not assuming that all the needed elements are existing. Please let me know if you need to alter the script in any other ways.

marringi
04-03-2008, 06:25 PM
Thanks, that works perfectly.

But one thing, say I wanted to add a extra column in front of the three columns that the scripts counts that would not count words, is that possible in this script?

When I added an extra column it showed 0 on the bottom..

Thanks again, Marringi

codeexploiter
04-04-2008, 03:39 AM
Here it is the thing you've mentioned. All the changes I've made in the previous source has been highlighted so that you can understand the difference without any trouble.

Important JS lines are also commented.



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">

</style>
<script type="text/javascript">
var rowCount = function(countWord){
/*I assume that the first row of the table will hold the column names and I've designed the script based on that if it is not like that it will differ somewhat*/
var rows = document.getElementById('tbl').getElementsByTagName('tr');
var avoidCol = [1]; //This is the array that will hold the column number which you want to avoid from counting use comma to seperate the values.
var cols = rows[1].getElementsByTagName('td').length;

if(avoidCol.length >= cols){return;}


var result = new Array(cols - avoidCol.length );

for(var m = 0; m < cols; m++){
result[m] = 0;
}

//Counting the total number mentioned word
for(var i = 1; i < rows.length; i++){
var tds = rows[i].getElementsByTagName('td');
for(var j = 0; j < tds.length; j++){
if(inArray(avoidCol, j + 1)) continue;
if(tds[j].childNodes.length == 1 && tds[j].childNodes[0].nodeValue == countWord)
result[j]++;
}
}
//Appending the result value in the last row's columns
var td = rows[rows.length - 1].getElementsByTagName('td');
for(var k = 0; k < td.length; k++){
if(inArray(avoidCol, k + 1)) continue;
td[k].appendChild(document.createTextNode(result[k]));
}
}
var inArray = function(arr,fval){
for(var i = 0; i < arr.length; i++){
if(arr[i] == fval)
return true;
}
return false;
}
window.onload = function(){
rowCount('Book'); //Pass the word which you want to count
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1" width="400" id="tbl">
<tr>
<th width="100">New Field</th>
<th width="100">First Field</th>
<th width="100">Second Field</th>
<th width="100">Third Field</th>
</tr>

<tr>
<td width="100">Book</td>
<td width="100">Mate</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Image</td>
<td width="100">Video</td>
<td width="100">Image</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100">Book</td>
<td width="100">Book</td>
<td width="100">Image</td>
<td width="100">Book</td>
</tr>

<tr>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</body>
</html>

marringi
04-05-2008, 10:19 AM
Thank you very much. Makes my life much easier!

Regards, Ingimar

marringi
05-02-2008, 10:53 AM
Hello again.

I have been trying to get the script to work with two words or more. Is that possible?

The thing is I got a database that has one, two or more words that are written as one (ex. HelloBigWorld, HelloBig or Hello). Then I use:

var str="HelloBigWorld";
document.write(str.replace(/(\S)([A-Z])/g,'$1 $2'));

to divide the words that have two or more capital letters.

Would it be hard to modify the script so that it could count numerous words?