Results 1 to 7 of 7

Thread: counting

  1. #1
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default counting

    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

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Hmmm, is this useful at all?:
    http://www.tizag.com/javascriptT/jav...ng-indexOf.php
    Hope this helps,
    Nile
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Here is a demo page.
    Code:
    <?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.

  4. The Following User Says Thank You to codeexploiter For This Useful Post:

    marringi (04-03-2008)

  5. #4
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

    Code:
    <?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>

  7. The Following User Says Thank You to codeexploiter For This Useful Post:

    marringi (04-05-2008)

  8. #6
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much. Makes my life much easier!

    Regards, Ingimar

  9. #7
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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?
    Last edited by marringi; 05-03-2008 at 12:23 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •