Dynamic Variable Names Coldfusion

旧巷老猫 提交于 2019-12-08 02:06:19

问题


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# & ":&nbsp;&nbsp;<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# & ":&nbsp;&nbsp;<input type='text' name='#catNameNoSpace#' value=" & getListingInformation.#catNameNoSpace# & "><br />">

回答1:


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]



回答2:


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




回答3:


Slightly different but might be useful to anyone looking at this: You can also use Variables["staticPartOfVariableName#DynamicPartOfVariableName#"] .




回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!