Posted By

nuukem on 06/23/12


Tagged

ColdFusion


Versions (?)

Creating Dynamic Variables


 / Published in: ColdFusion
 

Create dynamic variables from a query or a list.

  1. <h3>From a Query</h3>
  2.  
  3. <cfquery name="settingsParameters" datasource="#dsn#">
  4. SELECT NAME, VALUE
  5. FROM MY_SETTINGS
  6. WHERE ACTIVE = 1
  7. AND SECTION = 'whatsNew'
  8. </cfquery>
  9.  
  10. <!--- To output the results use one of these 2 mothods. The latter is the newer, simpler way to write it --->
  11. <cfloop query="settingsParameters">
  12. <cfset temp2 = SetVariable("settings.#settingsParameters.name#", settingsParameters.value) />
  13. </cfloop>
  14.  
  15. <!--- OR, CF >= 8 (i think) --->
  16.  
  17. <cfloop query="settingsParameters">
  18. <cfset "settings.#settingsParameters.name#" = settingsParameters.value >
  19. </cfloop>
  20.  
  21.  
  22. <h3>From a list</h3>
  23.  
  24. <cfset myList = "something=blah;what=now;who=jim">
  25.  
  26. <!--- Loop over list breaking it up on semicolon. create a temp array and use array[1] for the name of the variable and array[2] as the value i.e. bupid=7; creates <cfset additionalParams.bupid = 7> --->
  27.  
  28. <cfloop list="#myList#" delimiters=";" index="i">
  29. <cfset k = listToArray(i,"=")>
  30. <cfset "additionalParams.#k[1]#" = k[2] \>
  31. </cfloop>

Report this snippet  

You need to login to post a comment.