Hey Guys, I'm having a tad of an issue dealing with Dynamic Variable Names. What is happening is I have a CFC that builds part of form for me using some data in a table. Then the cfc sends the form's code back to the page as a string. Well I need to assign values to these form fields so people don't overwrite the data. I'm pulling the data in the function in the cfc. So I'm trying to throw this dynamic variable into the string and it is messing things up for me. I keep getting an error saying
A CFML variable name cannot end with a "." character.
Here is the code I'm using that gives me the error. I'm not all too experienced with programming I haven't been doing this too long. So ANY input would be awesome.
<!--- ================================================================== --->
<cfargument name="catFormQuery" type="query" required="yes">
<cfargument name="listingID" required="yes">
<cfset var getListingInformation = "">
<cfset var returnVar = "">
<cfset var fieldValue = "">
<cfset var catNameNoSpace = "">
<!--- get the listing Information --->
<cfquery name="getListingInformation" datasource="backEndDSN">
Select * from listings
where listingID = #arguments.listingID#
</cfquery>
<cfoutput query="arguments.catFormQuery">
<!---====================--->
<!--- Set catNameNoSpace --->
<!---====================--->
<cfset catNameNoSpace = replaceNoCase(arguments.catFormQuery.catName, " ", "_")>
<!---==========--->
<!--- for text --->
<!---==========--->
<cfif arguments.catFormQuery.catType eq 'text'>
<cfset returnVar = returnVar & #arguments.catFormQuery.catName# & ": <input type='text' name='#catNameNoSpace#' value=" & getListingInformation.#catNameNoSpace# & "><br />">
</cfif>
So anyway if you can give me any input or advice that would be great. Thanks a lot.
The code is right down here at the bottom.
<cfset returnVar = returnVar & #arguments.catFormQuery.catName# & ": <input type='text' name='#catNameNoSpace#' value=" & getListingInformation.#catNameNoSpace# & "><br />">
This definitely won't work, it's not valid CFML:
getListingInformation.#catNameNoSpace#
Evaluate is the devil, but you can use the array-style syntax instead. The only caveat is that you need to explicitly specify the row from which you want the value to come (and if the query has no rows, this will error out).
getListingInformation[catNameNoSpace][1]
Sixten's answer has a syntax you can use, but you'll still need to watch out for illegal characters in variable names as answered elsewhere. The ultimate guide for variables is here: http://www.depressedpress.com/Content/Development/ColdFusion/Articles/Variables/Index.cfm, especially this section http://www.depressedpress.com/Content/Development/ColdFusion/Articles/Variables/NotationIndexed.cfm
Slightly different but might be useful to anyone looking at this: You can also use Variables["staticPartOfVariableName#DynamicPartOfVariableName#"] .
Alright I think I figured it out. I don't really like how I had to do it though.
evaluate("getListingInformation.#catNameNoSpace#")
I've heard somewhere before that using evaluate is slow and not very clean. Is there a better option?
来源:https://stackoverflow.com/questions/1719947/dynamic-variable-names-coldfusion