Setting session varialbes with dynamic names

Discussion in 'Programming' started by tbarr60, Jun 25, 2007.

  1. #1
    I am trying to set session variables for a list of varialbes and get the following error. I tried a number of things but it keeps balking at a period in the variable name.

    A CFML variable name cannot end with a "." character.
    The variable session. ends with a "." character. You must supply an additional structure key or delete the "." character.

    The CFML compiler was processing:

    * a cfset tag beginning on line 21, column 18.
    * a cfset tag beginning on line 21, column 18.


    The error occurred in C:\CFusionMX7\wwwroot\tools\b-b\qry_b-b.cfm: line 21

    19 : <cfoutput>
    20 : <cfloop query="bb_log">
    21 : <cfset thisfield = session.#field#>
    22 : <cfset #thisfield# = #newval#><br>
    23 : </cfloop>
    The part I am not clear on is the "You must supply an additional structure key".
     
    tbarr60, Jun 25, 2007 IP
  2. Paul_Hopkinson

    Paul_Hopkinson Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    does the var #field# have a value in it?

    If not then you will get this error
     
    Paul_Hopkinson, Jun 25, 2007 IP
  3. tbarr60

    tbarr60 Notable Member

    Messages:
    3,455
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    210
    #3
    #field# will always have a value.
     
    tbarr60, Jun 25, 2007 IP
  4. Paul_Hopkinson

    Paul_Hopkinson Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you need to use this instead...

    <cfset thisfield = "session.#field#">
     
    Paul_Hopkinson, Jun 26, 2007 IP
  5. newbish

    newbish Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You will need to lock/unlock the variable correctly but this is the syntax I used to create dynamic session variables.

    <cfset id = 1>
    <cfset temp = SetVariable("Session.Stuff#id#", "Some Value");>

    retrieval is similar

    <cfset someVar = Evaluate("Session.Stuff#id#");>
     
    newbish, Jun 26, 2007 IP
  6. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Or you can use use array syntax

    <cfset thisfield = session[field]> ...or ...
    <cfset thisfield = session["someID#Id#"]>


    1) Generally you should avoid evaluate() when possible
    2) You don't need pound signs here:
    <cfset #thisfield# = #newval#>

    It won't break anything, but its not needed. Instead use:
    <cfset thisfield = newval>
     
    cfStarlight, Jun 26, 2007 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    Agreed with STarlight. That method is the best, and most organizing method. When setting dynamic variables, always use brackets around it rather then periods (this goes with any structure, array, or list).
     
    drewbe121212, Jun 27, 2007 IP