Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a count of Columns in a database 1

Status
Not open for further replies.

jeffwest2

MIS
Feb 5, 2009
64
0
0
GB
I have a database linked to some fields on a html page, however, I could have up to 28 sets of populated data, or as few as 1, what I need is a simple piece of code along the lines that will count along the dataline all instances where a field called Firstname is not null (i.e. poulated), then this count to go into a field on the html page, as well as disable/enable some buttons (1 - 20), this being done on the onload event of the page, so i guess it needs to be a function, but can't seem to get the syntac right to do this.

The disable/enable bit I can do, it is just the function/return count I need help with, any help would be greatly appreciated.

Regards

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
I'm not sure I completely understand, but from what I can gather something like this may work:

Code:
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]countFields[/color][/b][COLOR=#990000]()[/color]
[COLOR=#FF0000]{[/color]
 [b][COLOR=#0000FF]var[/color][/b] fields [COLOR=#990000]=[/color] document[COLOR=#990000].[/color][b][COLOR=#000000]getElementsByName[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'Firstname'[/color][COLOR=#990000]);[/color]
 [b][COLOR=#0000FF]var[/color][/b] theCount[COLOR=#990000]=[/color][COLOR=#993399]0[/color][COLOR=#990000];[/color]
 [b][COLOR=#0000FF]for[/color][/b][COLOR=#990000]([/color][b][COLOR=#0000FF]var[/color][/b] i[COLOR=#990000]=[/color][COLOR=#993399]0[/color][COLOR=#990000];[/color]i[COLOR=#990000]<=[/color]fields[COLOR=#990000].[/color]length[COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000];[/color]i[COLOR=#990000]++)[/color]
 [COLOR=#FF0000]{[/color]
 [tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]fields[COLOR=#990000][[/color]i[COLOR=#990000]].[/color]value[COLOR=#990000]!=[/color][b][COLOR=#0000FF]null[/color][/b][COLOR=#990000])[/color]
 [tab][COLOR=#FF0000]{[/color]
 [tab][tab] theCount[COLOR=#990000]++;[/color]
 [tab][COLOR=#FF0000]}[/color]
[COLOR=#FF0000]}[/color]
document[COLOR=#990000].[/color][b][COLOR=#000000]getElementById[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'placeforcountID'[/color][COLOR=#990000]).[/color]innerHTML [COLOR=#990000]=[/color] theCount[COLOR=#990000];[/color]

Code:
[COLOR=#990000]<[/color]body onload[COLOR=#990000]=[/color][COLOR=#FF0000]"countFields();"[/color][COLOR=#990000]>[/color]
[COLOR=#990000]...[/color]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil

Thanks for this, just one quick query, below is the only slightly amended code,

Code:
function countfields()
{
 var fields = document.getElementById('Con2');
 var theCount=0;
 for(var i=0;i<=fields.length-1;i++)
 {
     if(fields[i].value!=null)
     {
          theCount++;
     }
}

document.getElementById('placeforcountID').innerHTML = theCount; 
}

The bit that i am having an issue with is the
Code:
document.getElementById('placeforcountID').innerHTML = theCount;
bit, am I assuming this is meant to send the count value to a field, but it keeps erroring with a very helpful 'Unknown Error', I can't seem to work out what the error actually means, I have created a field with the name and id of 'placeforcountID', but it still errors.

Any further help you can give I would be really grateful.

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
If by field you mean an input say <input type="text" ...> the no no.

The .innerHTML property applies to most HTML elements, like divs, p, td, etc...
For an input you can use the value property.

document.getElementById('placeforcountID').value = theCount;

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil, Great thanks for that, i was being a bit dense and should have worked that out for myself.

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
Although at first I thought it was working, it isn't, no matter what I do, it just isn't counting the field, either if it does have something in or doesn't :-(

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
Well what is it doing? Do you get an error? Try adding some alerts to debug.

Code:
function countFields()
{
[COLOR=#A40000] alert('running Counter function');[/color]
 var fields = document.getElementsByName('Firstname');
[COLOR=#A40000] alert(The number of fields Returned is: " + fields.length);[/color]
 var theCount=0;
 for(var i=0;i<=fields.length-1;i++)
 {
     if(fields[i].value!=null)
     {
          theCount++;
     }
}
document.getElementById('placeforcountID').innerHTML = theCount;


Step through the code, make sure its actually running. Also As i said above I wasn't entirely sure what you meant, so I put this together based on what I understood, there's a chance its not what you need.

If you shows an example of your HTML that would need to be counted maybe I or someone else here can come up with a better solution or tweak this one.

But without knowing what's going on its hard to offer a fix.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil.

Sorry, you are right, I am not explaining myself very well.

Basically I have a series of html pages, on which I have a series of buttons and hidden text fields, based on if the fields have data in them (First name) from a database table, I want to be able to disable/enable a series of buttons, this allows users to update information within each page.

I have managed to do this via some code checking the fields (1-10) however I could have up to 28 sets of data, and want to try and streamline the code to basically count the number of fields that have something in and thereby just enable x number of buttons across three seperate pages (buttons 11-20 and 21-30 would only be available if there are that many fields with data in), a single button 11-20 would be enabled if field 11 has something in to open the page for 11-20, button 21-30 would be enabled if field 21 had something in and again a single button enable that.

Currently I am using an onload event that checks each individual hidden field and if it doesn't have anything in then Button1..2..3 etc is disabled, else it is enabled, which while it works it is a lot of coding which isn't really very tidy or efficient.

Hopefully that makes a bit more sense of what i am trying to aceive.

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
If the fields are in separate pages how is Js supposed to know how many they are across the pages? JS runs on a single page, it can;t go into other pages to look for elements.

Perhaps it would be easier to simply return form your server side a code the row count from your query, and use that to base your enabling or disabling of buttons. alternatively having your server side query create the fields as necesary rather than having them prexiostant, and then populated would be a better approach.

Going back to my code, it would work if all hidden inputs were in the same page as the counting code, and if the hidden fields are all named Firstname. not FirstName nor First Name nor firstname.

The only thing the code is doing is get a list or collection of elements who's name is "Firstname". It doesn't differentiate between hidden inputs and non hidden inputs or even other elements that may have that same name. When it has the collection from the getElementsByName function, then it runs through them checking to see if they have values different to Null. If they do, it increases the counter. At the end just printing the counter to an element.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Sorry, I think i mis understood how this works, I basically have fields called something different.

I need to name them the same, then the code will cyclce through them counting those that are not null.

This will then add a number to my field, i then just need to take that value and if it is 0 then sets no button to active, otherwise the number of active buttons will equal the number in my value.

If this is greater than 10, then my 11-20 button will become active, giving the user the access to the pages for this, if it is greater than 20, then the button for 21 - 28 will become active.

That makes a lot more sense now having worked this through.

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
Got it working by changing the code a little.

Code:
function countfields()
{ 
 var fields = document.getElementById('FN').value; 
 var theCount=0;
 var i
 for(var i=0; i<< fields.length;i++)
    {
     if(fields[i].value!="")
     {
          theCount++;
     }
}
document.getElementById('placeforcountID').value = fields.length; 
  Disable()

Thanks for all your help.

Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
 
I'd be very surprised if that last code worked at all. << is a bitwise operator, not a comparison

you also seem to be giving more than one DOM node the same id ('FN'). this is not advised.
 
As would I.

This:

var fields = document.getElementById('FN').value;

Would return a single item's value as getElementById only ever returns a single element. So unless your value has an array of items there, this is not going to work.

Its just counting the amount of characters in the string value of the element returned.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top