Ask a Question related to Coldfusion Database Access, Design and Development.
-
surenr #1
Dynamic Attributes
Is it possible to read dynamic attributes into an array?For example: i have the
attributes as
ans_1,ans_2,ans_3,ans_4,..... Depending on the user coldfusion should pull out
values of the ans.I had used the below code:
<cfif #user# EQ 'x'>
<cfloop index="loop" from="1" to="40">
<cfset student_ans[loop]=#stud_ans."ans_" & loop#>
</cfloop>
<cfelse>
<cfloop index="loop" from="1" to="80">
<cfset student_ans[loop]=#stud_ans."ans_" & loop#>
</cfloop>
</cfif>
from the above code codefusion should pull out the first 40 values from the
server else it should pull out 80 values from the server.any idea or
suggestions are welcome.thnx in advance
surenr Guest
-
add attributes to tag
What files do i look for to modify to add attributes to a tag? I want it to suggest more attributes when i open specific tags. I've been trying to... -
Missing Attributes?
I have been following some tutorials online and have noticed that some of the code in tutorials do not compile in the latest versions of... -
Dynamic <INPUT name=" "> attributes
Hello everyone. I seem to be in a rut on a local intranet web application im working on. I am developing a questionaire for a portion of the... -
Dynamic Security Attributes?
Is it possible to add declarative security attributes to classes or methods at runtime? I am toying with the idea of a class factory that emits... -
Attributes
Hi All... I have a Auditing system. I want to send Audit messages using attributes. I managed to activate the ContextAttribute object, BUT it... -
mxstu #2
Re: Dynamic Attributes
You need to use the evaluate() function to get the value of the dynamic
attribute
<cfset someDynamicVariable_123 = "Today is "& DateFormat(Now(), "mm/dd/yyyy")>
<cfset loop = "123">
<cfset value = evaluate("someDynamicVariable_"& loop)>
<cfoutput>#value#</cfoutput>
mxstu Guest
-
cjeris #3
Re: Dynamic Attributes
In case the dynamic variables reside in a structure (as in the OP's example)
one can do this without the Evaluate function, using the bracket notation for
accessing structure members:
<CFLOOP index="loop" from="1" to="40">
<CFSET student_ans[loop] = stud_ans["ans_#loop#"]>
</CFLOOP>
cjeris Guest
-
mxstu #4
Re: Dynamic Attributes
surernr,
If the dynamic attributes are in a structure, then cjeris's method would be
better as it avoids an unnecessary call of the evaluate() function.
<CFSET student_ans[loop] = stud_ans["ans_"& loop]>
mxstu Guest
-
surenr #5
Re: Dynamic Attributes
Suppose that the table I am trying to access data looks like the below one and
the name of the table is Student_Answers:
Userid Ans_1 Ans_2 Ans_3 ??. Ans_39 Ans_40
James A B A ??. E D
John B C A ??. E A
Ben A D A ??. E A
Toni C A E ??. A A
To access the attributes dynamically I had tried the below code:
<cfquery name = ?Stud_Answers? Datasource =?XYZ?>
SELECT * FROM Student_Answers
</cfquery>
<cfif #userid# EQ 'James'>
<cfloop index="loop" from="1" to="20">
<cfset student_ans [loop] = #Stud_Answers."ans_" & loop#>
</cfloop>
<cfelse>
<cfloop index="loop" from="1" to="80">
<cfset student_ans [loop] = #Stud_answers."ans_" & loop#>
</cfloop>
</cfif>
But coldfusion would not recognize the dynamic attribute from the tables that
is ans_1, ans_2, ans_3 and so on.
surenr Guest
-
surenr #6
Dynamic Attributes
Suppose that the table I am trying to access data from the tables that look
like the below and the name of the table is Student_Answers:
Userid Ans_1 Ans_2 Ans_3 ??. Ans_39 Ans_40
James A B A ??. E D
John B C A ??. E A
Ben A D A ??. E A
Toni C A E ??. A A
To access the attributes dynamically I had tried the below code:
<cfquery name = ?Stud_Answers? Datasource =?XYZ?>
SELECT * FROM Student_Answers
</cfquery>
<cfif #userid# EQ 'James'>
<cfloop index="loop" from="1" to="20">
<cfset student_ans [loop] = #Stud_Answers."ans_" & loop #>
</cfloop>
<cfelse>
<cfloop index="loop" from="1" to="40">
<cfset student_ans [loop] = #Stud_answers."ans_" & loop #>
</cfloop>
</cfif>
But coldfusion would not recognize the dynamic attribute from the tables that
is ans_1, ans_2, ans_3 and so on.I did try the evaluate funtion too but it did
not seem to work.
Any idea or suggestions.
thnx in advance.
surenr Guest
-
TA-Selene #7
Re: Dynamic Attributes
Try this:
#Evaluate("Stud_Answers.Ans_" & loop)#
TA-Selene Guest
-
mxstu #8
Re: Dynamic Attributes
Since you appear to be using a single dimension array, I am assuming your query
only returns one record. In this case you could use something like:
<cfset student_ans = ArrayNew(1)>
<cfloop index="loop" from="1" to="20">
<cfset student_ans [loop] = Stud_Answers["ans_" & loop][1]>
</cfloop>
<cfdump var="#student_ans#">
Where the "1" represents the query row number. You could also use the
evaluate() function:
<cfloop index="loop" from="1" to="20">
<cfset student_ans [loop] = evaluate("Stud_Answers.ans_" & loop &"[1]")>
</cfloop>
<cfdump var="#student_ans#">
mxstu Guest
-
surenr #9
Re: Dynamic Attributes
thnx mxstu,
your code did work, never knew about this function evaluate.
surenr
surenr Guest



Reply With Quote

