Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting the NumberofColumns in a resultset

New Here ,
Feb 26, 2010 Feb 26, 2010

So I have a query that creates a table of the data after it is submitted, but the results are dependent upon which SELECT fields the user selects via a checkbox.

Example:

I have 3 checkboxes for 3 different fields in the query: Name, Number, Address.  If the user only checks Name and Number, then the SELECT query will be SELECT firstName, number FROM tableofinfo.

So, the resulting table for the results can either be 1, 2 or 3 columns (depending on how many checkboxes they check).  My question is, how can I pull the number of fields or columns that the table resulted in so that I can use that for further manipulation?  So, for the example it would be 2 (Name and Number).  I have the result="resultInfo" set on the <cfquery> tag, and I know that #resultInfo.RecordCount# will give me the number of rows, but I want the number of columns.  Is this possible?

Thanks

549
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 26, 2010 Feb 26, 2010

cfquery provides a couple of variables.  One of these lists all the columns.  The name of that variable is in the cfml reference manual.  If you don't have one, google cfquery and you'll find what you want lickety split.

If you don't know what to do with that variable, googling "coldfusion list functions" might give you some ideas.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2010 Feb 26, 2010

Yea, columnList is the one you're talking about.  Is there a function that will count resultInfo.ColumnList as an integer?  To the effect of count(resultInfo.ColumnList)?  Something like that where it will provide an integer of 1, 2 or 3?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 26, 2010 Feb 26, 2010

How did you make out with the 2nd paragraph of my previous answer?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2010 Feb 26, 2010

Ok so I think I got it.  I was able to use Len() and then insert the form variables (they are checkboxes with values of 1 or 0) so if I do <cfset len = #Form.var1# + #Form.var2#>  I can get a variable #len# that will tell me how many columns the user chose.

Thanks for the push!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 26, 2010 Feb 26, 2010

A more scalable solution might be to use ListLen(qMyQuery.ColumnList) - this should give you the number of columns returned by a query, if that's what you're after.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 26, 2010 Feb 26, 2010

Yes I tried that. But the problem is, is that ListLen gives the length

returned by the query. BUT, the way I have it setup is, the query returns

about 17 fields, and the user selects which fields he or she wants to view.

So if only 4 are checked, then the table will only show 4 even though 17

were queried. And what I was trying to do was dependent on the TABLE

columns and not the SELECT columns.

Thanks for your help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 26, 2010 Feb 26, 2010

I interpreted your initial post to be that the number of fields you select depended on the form submission.  That would actually be more efficient.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 26, 2010 Feb 26, 2010
LATEST

You're building this table somehow...  Cant you just set a counter and increment it when you add a column to the table?

Or, maybe use the submitted form data to determine how many?

D?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources