Professional Web Applications Themes

Setting a variable name with a variable in a loop - Coldfusion - Advanced Techniques

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

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

  3. #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" <webforumsusermacromedia.com> wrote in message
    news:d17jbe$htd$1forums.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

  4. #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

  5. #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

Similar Threads

  1. Creating variable using loop
    By charmed0rz in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 13th, 07:53 AM
  2. Using Loop Index in variable
    By Stressed_Simon in forum Macromedia ColdFusion
    Replies: 3
    Last Post: April 29th, 05:55 AM
  3. Newbie : Incrementing variable in a loop
    By Tony in forum PHP Development
    Replies: 2
    Last Post: November 22nd, 11:10 AM
  4. Setting "variable" global variable ?
    By Johan Holmberg in forum Ruby
    Replies: 9
    Last Post: September 25th, 03:42 PM
  5. Replies: 4
    Last Post: September 18th, 08:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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