dynamic variable name and output problem

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default dynamic variable name and output problem

    Hey all,
    I having trouble with dynamically naming a variable and then outputting the
    value. If I hard code the output blick below to #scoreAvg#, it outputs '0'.
    If anyone could tell me how to get the below code to output '0' and not the
    term 'scoreAvg' I would really appreciate it.

    Thanks in advance,
    Bart

    <cfset i = "score">
    <cfset '#i#Avg' = 0>
    <cfoutput>#i#Avg</cfoutput>

    megalith Guest

  2. Similar Questions and Discussions

    1. Dynamic Output
      Thanks in advance for any help. Here is what I am trying to accomplish with the code below. In the first query "Traffic01" I am getting a list...
    2. Help with dynamic output using Form.variable
      Well here is my situation: Survey answers insert into access db. Working fine. Viewing the results: I have a form that you can choose by who...
    3. simple output message to variable question
      sounds simple enough, i want to turn traced output messages into variables, but is there a way to read the output of one layer from a different...
    4. output variable to txt file
      I made a flash program that finds prime numbers. These prime numbers are put into a string which gets longer and longer vPrimeString =...
    5. Can't output a variable to HTML
      I have this code on a page: <p class="appTitle"> <?php echo "$appTitle"; ?> </p> But the resulting HTML is: <p class="appTitle"> </p>
  3. #2

    Default Re: dynamic variable name and output problem

    As usual the answer was pretty simple, I just had to change the output to
    <cfoutput>#evaluate('#i#Avg')#</cfoutput>
    megalith Guest

  4. #3

    Default Re: dynamic variable name and output problem

    You don't need to use evaluate() here.

    Use
    <cfset VARIABLES[i & "Avg"] = 0>

    and

    <cfoutput>#VARIABLES[i & "Avg"]#</cfoutput>

    evaluate() is slower to process and has security issues!
    Stressed_Simon Guest

  5. #4

    Default Re: dynamic variable name and output problem

    Thanks Simon
    I never new that existed and can't find it in my CFWACK book (although it is
    probably somewhere).
    Out of curiosity, and if you have time, what are the security issues with
    Evaluate and other uses for Variables?

    thanks for your reply,
    Bart

    megalith Guest

  6. #5

    Default Re: dynamic variable name and output problem

    When you set a local variable ie <cfset myVar = "Simon"> you are really setting
    VARIABLES.myVar = "Simon" the variables scope is the only scope that you can
    usually get away with not specifying.

    So this is actually a Stucture called VARIABLES. You can reference structure
    keys in two ways using dot notiation (VARIABLES.myVar) or you can use Array
    Notation (VARIABLES["myVar"]) these are identical. But as I mentioned before
    the array notation is very handy for dynamically named vars.

    Basically evalute() has security issues for example if you pass a FORM or URL
    variable that is a CF function it will run it (BAD NEWS). So I never use it,
    generally if you have to use Evaluate() you are doing something wrong.

    HTH

    Stressed_Simon Guest

  7. #6

    Lightbulb Re: dynamic variable name and output problem

    Thank you, Stressed_Simon!

    I was able to use the VARIABLES[varname] technique to pass a series of list loop index values into a SQL query. The variable must be first referenced in a string variable; it cannot be included directly in the cfquery SQL statement. I tried many ways without success until I read your post.

    Hope this helps someone...

    My example outputs employee directory information grouped by role. It loops through three roles in the role_list. Within each role, it loops through employee IDs from matching lists using the SQL IN operator. Note the employee IDs were necessarily in lists because they were a handpicked group not queryable by department, etc.

    <cfset role_list = "Faculty,Staff,Student_Employees">
    <cfset Faculty = "1529, 156, 1096, 1964, 1137, 23, 105">
    <cfset Staff = "27, 2039">
    <cfset Student_Employees = "2339, 2340, 2338">

    <cfloop index="thisRole" list="#role_list#">

    <div class="profiles">
    <a name="<cfoutput>#thisRole#</cfoutput>"></a>
    <h2><cfoutput>#replace(thisRole,"_"," ","ALL")#</cfoutput></h2>

    <cfset sqlString = "
    SELECT tblEmployee.*
    FROM tblEmployee
    WHERE tblEmployee.employee_ID IN (" & #VARIABLES[thisRole]# & ")
    ORDER BY tblEmployee.last_name, tblEmployee.first_name
    ">

    <cfquery name="getEmployeeDetails" datasource="directory">
    #sqlString#
    </cfquery>

    ...

    </cfloop>

    ...
    Unregistered 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