Skip to main content
Known Participant
July 11, 2008
Question

Dynamic Variables

  • July 11, 2008
  • 8 replies
  • 1449 views
I want to display the value of a dynamically generated variable...I can't figure out how to do it, is it even possible?

<cfset variable_name = "red">

<cfset "variable_color_#variable_name#" = "The color is red">

<cfset full_variable = "variable_color_#variable_name#">

I want the output of "full_variable" to be the value of the dynamic variable created, "The color is red". Is it possible?

Thanks,
Paul
    This topic has been closed for replies.

    8 replies

    Inspiring
    July 18, 2008
    paulferree wrote:
    > Aha...got it!
    >
    > I had to do it this way:

    Well no you didn't have to do it that way, CF lets you do it that way,
    but many would find it confusing and unnecessary.

    You only need the quotes with string literals, if you are using
    variables that contain a string, then just put the variable name in
    without the quotes or the hashes|pound signs.

    To reiterate my earlier examples that demonstrated exactly this:

    <cfoutput>#VARIABLES['getcategory']['cat_id'][1]#</cfoutput>

    OR

    <cfset myQuery = 'getcategory'>
    <cfset myColumn = 'cat_id'>
    <cfoutput>#VARIABLES[myQuery][myColumn][1]#</cfoutput>

    Now really; don't you find that a cleaner line of CFML code to read. At
    least I do. :-)
    Known Participant
    July 22, 2008
    Well, I hope I'm not beating this thing to death. If someone could point me in a direction where I can figure this out on my own, I'd be grateful.

    The next little bump for me is pulling the recordcount of a query thats name is dynamic. So in an if statement I have:

    <cfif VARIABLES[checkmatch_query][recordcount] GT 0>

    That barks at me.

    Where in the scope is the query info stored??

    This is really helping me clean up some code so I really appreciate the help. I just wish I could wrap my head around it and figure it out myself, I don't know why I'm having a hard time with it.

    Thanks!
    Paul
    Known Participant
    July 18, 2008
    Aha...got it!

    I had to do it this way:

    <cfoutput>#VARIABLES['#query_name#']['#column_name#'][1]#</cfoutput>

    perfect!

    Thanks again guys.
    Known Participant
    July 12, 2008
    This is a delete function that every module in my CMS uses. I just include a template that contains all my admin functions in each module.

    I have 2 types of variables for this function, 1 for the variable names, and 1 for the actual values. The names are not part of the form. But their values are being passed from the forms.

    Thanks!
    Paul
    Known Participant
    July 18, 2008
    Ok...now I need to do the same thing, but reference a query scope instead of a form or variable scope.

    If I have a query:

    <cfquery name="getcategory" datasource="#DSN#">
    SELECT cat_id FROM tblCategories
    </cfquery>

    How do I pass through the query scope and variable name and return the value of it?

    Paraphrased:

    query_name = "getcategory"
    query_variable = "cat_id"

    Return the value of #query_name#.#query_variable#


    Thanks,
    Paul
    Participating Frequently
    July 18, 2008
    quote:

    How do I pass through the query scope and variable name and return the value of it?
    You could use the session scope:

    <cfquery name="getcategory" datasource="#DSN#">
    SELECT cat_id FROM tblCategories
    </cfquery>

    <cfset session.category_qry= getcategory >

    Then, something like this ....

    <cfoutput query="session.category_qry">
    #cat_id#
    </cfoutput>

    etc...

    Phil
    Inspiring
    July 12, 2008
    If your function is on your action page, and it is only being called once, making it a function is a waste of code. Just put the stuff inside your function into your template.

    If the function is in an included file, your code might work. If it's in a cfc, it won't because the cfc won't know about the form. It will only know what arguments you pass it.

    If all your arguments come from a form, argumentcollection="#form#" will save you some typing.
    Inspiring
    July 12, 2008
    Why are you using the form scope instead of the arguments scope in your function?
    Known Participant
    July 12, 2008
    Ahh...I get it now. The variable scope is actually a structure and we're calling the value of an element within that structure.

    Here's how I've used it FYI, it's a imply dynamic delete function and I was able to remove the 2 arguments that specified the values, thanks!

    <cffunction name="delete_step1" returntype="any" output="yes">
    <cfargument name="page_return" type="string" required="yes">
    <cfargument name="table_primkey_name" type="string" required="yes">
    <cfargument name="table_title_column" type="string" required="yes">

    <cfset table_primkey_value = "#form[table_primkey_name]#">
    <cfset table_title_column_value = "#form[table_title_column]#">

    <cfoutput><div id="delete_warning">Are you sure you want to delete: #table_title_column_value# ?<Br /><br />
    <cfform action="#page_return#" method="post" enctype="multipart/form-data">
    <input name="submit" type="submit" value="Yes, Confirm Deletion" /> 
    <input name="submit" type="submit" value="Cancel" />
    <input type="hidden" name="#table_primkey_name#" value="#table_primkey_value#" />
    <input type="hidden" name="#table_title_column#" value="#table_title_column_value#" />
    </cfform>
    </div>
    </cfoutput>
    </cffunction>

    Thanks again,
    Paul
    Known Participant
    July 12, 2008
    This is a function that is called after a delete button is pressed in a form.

    The actual form variables (the primary key column name, e.g. category_id, and the title column name, e.g. category_name) are passing to the page that is calling this function so they are available for me to utilize...however, these column names are unknown to the function so the actual form variable name is an unknown and needs to be dynamic. The values were there, I just didn't know how to grab them.

    So, all I need to do is pass the correct variable name to the function (e.g "table_primkey_name") and then address it in the FORM scope and I'll have the correct value.

    Before this, I was having to retrieve the form variables first and then pass them through the function. Doing it this way saved about 10 lines of code and the hassle of making sure all my variables were right.

    So it is actually a variable in the form scope, not the arguments. Unless I'm missing something else. Which very well could be the case, I'm a very novice CF developer.

    Paul
    Inspiring
    July 11, 2008
    #variables[full_variable]# or something like that should work.
    Known Participant
    July 12, 2008
    AWESOME!!! Thanks Dan, that worked wonderfully! Is there a quick explanation on why it worked? What do the brackets do?

    I need this because I'm creating some functions and I have some FORM. scoped variables that I want to be dynamic and the way I was originally doing it, I'd get <input value="#form.variable#"> instead of the actual value of that form variable. Don't know if that makes any sense, but this just saves me from passing 2 more arguments through a function...and that, I like!!!

    Paul
    Inspiring
    July 11, 2008
    paulferree wrote:
    > I want the output of "full_variable" to be the value of the dynamic variable
    > created, "The color is red". Is it possible?

    You maybe able to accomplish what you desire with the right combination
    of the evaluate() and|or the de() "delay evaluate" functions.

    But 99.734% of the time I have encountered a requirement like this, some
    exploration of the reasons behind the requirement yield a better
    solution then trying to force a language to recursively parse lines of code.