Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
How did you make out with the 2nd paragraph of my previous answer?
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?