Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

  7. #6

    Default 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

  8. #7

    Default Re: Dynamic Attributes

    Try this:

    #Evaluate("Stud_Answers.Ans_" & loop)#


    TA-Selene Guest

  9. #8

    Default 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

  10. #9

    Default Re: Dynamic Attributes

    thnx mxstu,
    your code did work, never knew about this function evaluate.
    surenr
    surenr Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139