Creating Dynamic Variables


/ Published in: ColdFusion
Save to your folder(s)

Create dynamic variables from a query or a list.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.