Setting a variable name with a variable in a loop

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

  1. #1

    Default Setting a variable name with a variable in a loop

    Here is the code that bombs: <cfloop index='x' from='1' to='10' step='1'>
    <cfif #evaluate('form.totalhours#x#')# eq 'r'> <cfset total_hours_#x# =
    #evaluate('form.remaining_#x#')#> <cfelse> <cfset total_hours_#x# =
    #evaluate('form.totalhours#x#')#> </cfif> </cfloop> I want to loop through
    and create up to 10 variables named total_hours_1 through total_hours_10. Then
    assign a value to them. The assigning of the value works, but the creating the
    variable names does not work. How do I work around this? (Of note, the
    variable name is used as the value of a db insert.)

    DAYohn Guest

  2. Similar Questions and Discussions

    1. Creating variable using loop
      Ok the first part of my code has a loop that loops 1-5 then i want it to set session variables but i don't know how to go about doing this.. i want...
    2. Using Loop Index in variable
      #form.rate#store## will throw an error as the sql query will get (assuming they selected 5):- 5store# Now as this is not surrounded by single...
    3. Newbie : Incrementing variable in a loop
      Hi, I have a Mysql row with several similar fields such as PA1, PA2, PA3 ... I would like to be able to loop through these fiels using a...
    4. Setting "variable" global variable ?
      Hi ! I wonder if there is any way in Ruby of setting a "variable" global variable at runtime, without using "eval". For example, could the...
    5. How to declare the result of a loop as a variable?
      I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop,...
  3. #2

    Default Re: Setting a variable name with a variable in a loop

    change <cfset total_hours_#x# = #evaluate("form.remaining_#x#")#> to:

    <cfset foo = evaluate("total_hours_#x# = form.remaining_#x#")>

    The foo is useless, your variable "total_hours_#x#" will be setup.

    Kronin555 Guest

  4. #3

    Default Re: Setting a variable name with a variable in a loop

    First off, you don't need most of those pound signs. Step="1" is default in
    cfloop, so don't really need that either.

    <cfloop index='x' from='1' to='10'>
    <cfset SetVariable("total_hours_" & x, IIf(Evaluate("form.totalhours" & x)
    EQ "r",'Evaluate("form.remaining_" & x)','Evaluate("form.totalhours_" &
    x)'))>
    </cfloop>

    SetVariable() is what you are looking for. And I used an Immediate If
    statement instead of the cfif/cfelse tags.

    --
    Ian O'Betz
    Clear Results
    [url]www.clearresults.net[/url]


    "DAYohn" <webforumsuser@macromedia.com> wrote in message
    news:d17jbe$htd$1@forums.macromedia.com...
    > Here is the code that bombs: <cfloop index='x' from='1' to='10' step='1'>
    > <cfif #evaluate('form.totalhours#x#')# eq 'r'> <cfset total_hours_#x# =
    > #evaluate('form.remaining_#x#')#> <cfelse> <cfset total_hours_#x# =
    > #evaluate('form.totalhours#x#')#> </cfif> </cfloop> I want to loop
    through
    > and create up to 10 variables named total_hours_1 through total_hours_10.
    Then
    > assign a value to them. The assigning of the value works, but the
    creating the
    > variable names does not work. How do I work around this? (Of note, the
    > variable name is used as the value of a db insert.)
    >

    Ian O'Betz Guest

  5. #4

    Default Re: Setting a variable name with a variable in a loop

    You can also do it this way without using Evaluate(). There is more overhead
    in using evaluate.



    <cfloop index="x" from="1" to="10">
    <cfif "form.totalhours#x#" eq "r">
    <cfset "total_hours_#x#" = form["remaining_#x#"]>
    <cfelse>
    <cfset "total_hours_#x#" = form["totalhours#x#"]>
    </cfif>
    </cfloop>

    eastinq Guest

  6. #5

    Default Re: Setting a variable name with a variable in a loop

    I discovered this works... <cfloop index='x' from='1' to='10' step='1'>
    <cfif #evaluate('form.totalhours#x#')# eq 'r'> <cfif
    #evaluate('form.remaining_#x#')# eq ''> <cfparam name='total_hours_#x#'
    default='#evaluate('form.remaining_#x#')#'> <cfelse> <cfparam
    name='total_hours_#x#' default='#evaluate('form.totalhours#x#')#'>
    </cfif> </cfloop> Thanks for all of the input. Alex

    DAYohn 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