2

嘿伙计们,我在处理动态变量名称时遇到了一点问题。发生的事情是我有一个 CFC,它使用表格中的一些数据为我构建了部分表单。然后 cfc 将表单的代码作为字符串发送回页面。那么我需要为这些表单字段分配值,这样人们就不会覆盖数据。我在 cfc 的函数中提取数据。所以我试图把这个动态变量扔到字符串中,这让我搞砸了。我不断收到错误消息

A CFML variable name cannot end with a "." character.

这是我正在使用的代码,它给了我错误。我对编程的经验并不多,我做这件事的时间也不长。所以任何输入都会很棒。

<!--- ================================================================== --->

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

所以无论如何,如果你能给我任何意见或建议,那就太好了。非常感谢。

代码就在底部。

                    <cfset returnVar = returnVar & #arguments.catFormQuery.catName# & ":&nbsp;&nbsp;<input type='text' name='#catNameNoSpace#' value=" & getListingInformation.#catNameNoSpace# & "><br />">
4

4 回答 4

13

这绝对行不通,它不是有效的 CFML:

getListingInformation.#catNameNoSpace#

评估是魔鬼,但您可以改用数组样式的语法。唯一需要注意的是,您需要明确指定要从中获取值的行(如果查询没有行,则会出错)。

getListingInformation[catNameNoSpace][1]
于 2009-11-12T06:18:11.783 回答
2

Sixten 的答案有一个您可以使用的语法,但您仍然需要注意变量名中的非法字符,正如其他地方所回答的那样。变量的终极指南在这里: http: //www.depressedpress.com/Content/Development/ColdFusion/Articles/Variables/Index.cfm,尤其是本节http://www.depressedpress.com/Content/Development/ColdFusion /文章/变量/NotationIndexed.cfm

于 2009-11-12T07:55:06.657 回答
1

略有不同,但可能对任何查看此内容的人有用:您也可以使用 Variables["staticPartOfVariableName#DynamicPartOfVariableName#"] 。

于 2016-06-17T17:25:07.207 回答
0

好吧,我想我明白了。不过,我真的不喜欢我必须这样做。

evaluate("getListingInformation.#catNameNoSpace#")

我之前在某处听说过使用评估很慢而且不是很干净。有更好的选择吗?

于 2009-11-12T06:08:53.893 回答