Creating Dynamic Variables

Submitted by:Jhon Jhon

Date added:20 October, 2012

Category:ColdFusion

Create dynamic variables from a query or a list.

Tags: dynamic variables

Code Snippet:

    <h3>From a Query</h3>

<cfquery name="settingsParameters" datasource="#dsn#">
SELECT NAME, VALUE
FROM MY_SETTINGS
WHERE ACTIVE = 1
AND SECTION = 'whatsNew'
</cfquery>

<!--- To output the results use one of these 2 mothods. The latter is the newer, simpler way to write it --->
<cfloop query="settingsParameters">
<cfset temp2 = SetVariable("settings.#settingsParameters.name#", settingsParameters.value) />
</cfloop>

<!--- OR, CF >= 8 (i think) --->

<cfloop query="settingsParameters">
<cfset "settings.#settingsParameters.name#" = settingsParameters.value >
</cfloop>


<h3>From a list</h3>

<cfset myList = "something=blah;what=now;who=jim">

<!--- 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> --->

<cfloop list="#myList#" delimiters=";" index="i">
<cfset k = listToArray(i,"=")>
<cfset "additionalParams.#k[1]#" = k[2] \>
</cfloop>
 
 

Comments