The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Accumulation items in a table
denmarks
post May 28 2012, 11:15 AM
Post #1


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



I have a table where one of the columns is activities. That field can contain any letters of the alphabet. There can be no duplicates within a cell but there can be none or any. There is a blank between each letter for readability. For example:
A C D F G
A B
[blank]
M N O R Z

What is the best way to scan the table and count how many of each letter appears? I assume creating an array and indexing by the letter may work best for the variables. I also need the best way to scan the table and the individual cell that would be compatible with multiple browsers.
Result
A - 2
B - 1
C - 1
etc
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 28 2012, 02:28 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(denmarks @ May 28 2012, 06:15 PM) *
That field

Do you mean a (form) field or a table cell?

Also I'm not sure I understand the purpose: does the site visitor type letters in a form field? (If OTOH you create the content yourself I don't see the point of a validation script. unsure.gif )

QUOTE
can contain any letters of the alphabet.

Do you mean a particular alphabet's charset? If any language's alphabet is allowed things may get hairy, if nothing else since you need a way for the script to separate various alphabets' characters from special characters).

QUOTE
I also need the best way to scan the table and the individual cell that would be compatible with multiple browsers.

Support in today's browsers shouldn't be a problem.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post May 28 2012, 06:37 PM
Post #3


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



This a table that I create but I constantly change. I wanted an automated way to count the letters.
This is for a group going on a cruise.
A - means they will attend event XXX
B - means they will attend event YYY
C - means they will attend event ZZZ

See http://hawaii.dmmarks.com

I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake.

I did find one method but it worked in Firefox and not in IE. Also the counting of the letters in a cell was too slow.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post May 28 2012, 10:51 PM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
This a table that I create but I constantly change.


"Change"? How, by some server script? If so then obviously the script should count the totals, which is easy. Using javascript is usually making things more difficult for yourself.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 29 2012, 01:14 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(denmarks @ May 29 2012, 01:37 AM) *

I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake.

It's relatively easy to find an occurence of say letter A in a cell, and then add up all such occurences. Duplicates can then be excluded from the count (that is, multiple A letters in a cell still just count as one).

Do you also want to remove duplicates in each cell for "esthetic" reasons? That's more tricky (=bug prone), especially if there are linebreaks etc in the code.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post May 29 2012, 01:52 PM
Post #6


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



QUOTE(Christian J @ May 29 2012, 11:14 AM) *

QUOTE(denmarks @ May 29 2012, 01:37 AM) *

I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake.

It's relatively easy to find an occurence of say letter A in a cell, and then add up all such occurences. Duplicates can then be excluded from the count (that is, multiple A letters in a cell still just count as one).

Do you also want to remove duplicates in each cell for "esthetic" reasons? That's more tricky (=bug prone), especially if there are linebreaks etc in the code.


There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter.

I need to do 2 things. I need a way to do a loop through the cells that will work for any browser. The contents of a cell will then be assigned to a variable. This is where I had a problem of it working for Firefox and not IE. I have found routines that will accumulate numbers in the cells and I tried to alter one of them. The problem occurred when trying to place the contents of the cell into a variable. It worked in Firefox but was null in IE. Sorry I did not keep the code.

Now I need the best way to accumulate the count of each letter. I do not want to create 26 if statements. There must be a more esoteric way to do a scan of the variable and for each letter add one to its corresponding accumulator. I believe that arrays can be indexed by a name (letter) rather than occurrence number. My first try was with 26 if statements and 26 variables. This is too much code. There must be an easier way.

Note that I do this for fun and as a learning experience, not because it must be done. Obviously I can continue to do the count manually. I only want to program it if it can be done with a relatively short routine.

Once I have the accumulation I have no problem putting it on the page. I do it with date counts now.

This post has been edited by denmarks: May 29 2012, 01:53 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 29 2012, 05:40 PM
Post #7


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



QUOTE(denmarks @ May 29 2012, 08:52 PM) *

There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter.

Then I misunderstood everything, I thought you wanted a kind of validation script to catch typos. Counting correct cell content is much easier.

QUOTE
I believe that arrays can be indexed by a name (letter) rather than occurrence number.

That's called an associative array. Here's an example that seems to work in my browsers:

CODE
<table border=1 id="memberTable">
<tr>
<th>foo</th>
<th>bar</th>
</tr>
<tr>
<td>text</td>
<td>A B C</td>
</tr>
<tr>
<td>text</td>
<td>B A</td>
</tr>
<tr>
<td>text</td>
<td>B</td>
</tr>
<tr>
<td>text</td>
<td></td>
</tr>
</table>

<p id="totals"></p>

<script type="text/javascript">
var cruise_events={'A':0, 'B':0, 'C':0}; // default values are zero for each cruise event

var table=document.getElementById('memberTable');
var totals=document.getElementById('totals');

for(var letter_item in cruise_events)
{
    var num=0; // tally
    for(var i=1; i<table.rows.length; i++)
    {
        // get cell content of last cell in row
        var row_cells=table.rows[i].cells;
        var last_cell=row_cells[row_cells.length-1];
        var cell_content=last_cell.innerHTML;

        // tally of each cruise event
        if(cell_content.indexOf(letter_item)!=-1)
        {
            num++;
        }
    }
    cruise_events[letter_item]=num;
}

// display results
for(var letter_item in cruise_events)
{
    totals.innerHTML+=letter_item+': '+cruise_events[letter_item]+'<br>';
}
</script>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post May 29 2012, 06:07 PM
Post #8


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



QUOTE(Christian J @ May 29 2012, 03:40 PM) *

QUOTE(denmarks @ May 29 2012, 08:52 PM) *

There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter.

Then I misunderstood everything, I thought you wanted a kind of validation script to catch typos. Counting correct cell content is much easier.

QUOTE
I believe that arrays can be indexed by a name (letter) rather than occurrence number.

That's called an associative array. Here's an example that seems to work in my browsers:

CODE
<table border=1 id="memberTable">
<tr>
<th>foo</th>
<th>bar</th>
</tr>
<tr>
<td>text</td>
<td>A B C</td>
</tr>
<tr>
<td>text</td>
<td>B A</td>
</tr>
<tr>
<td>text</td>
<td>B</td>
</tr>
<tr>
<td>text</td>
<td></td>
</tr>
</table>

<p id="totals"></p>

<script type="text/javascript">
var cruise_events={'A':0, 'B':0, 'C':0}; // default values are zero for each cruise event

var table=document.getElementById('memberTable');
var totals=document.getElementById('totals');

for(var letter_item in cruise_events)
{
    var num=0; // tally
    for(var i=1; i<table.rows.length; i++)
    {
        // get cell content of last cell in row
        var row_cells=table.rows[i].cells;
        var last_cell=row_cells[row_cells.length-1];
        var cell_content=last_cell.innerHTML;

        // tally of each cruise event
        if(cell_content.indexOf(letter_item)!=-1)
        {
            num++;
        }
    }
    cruise_events[letter_item]=num;
}

// display results
for(var letter_item in cruise_events)
{
    totals.innerHTML+=letter_item+': '+cruise_events[letter_item]+'<br>';
}
</script>



THANKS
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th March 2024 - 09:49 PM