如何使用coldfusion读取/循环URL中的数组

时间:2015-12-13 19:46:52

标签: arrays coldfusion

我在尝试使用jQuery sortable时尝试读取一些url-parameters /数组: https://linssen.me/entry/extending-the-jquery-sortable-with-ajax-mysql/

到目前为止,我还没弄清楚如何使用coldfusion循环使用url中的参数:

http://www.domain.com/update_sort.cfm?listItem[]=1&listItem[]=3&listItem[]=4&listItem[]=2

我非常感谢一些帮助/提示。

2 个答案:

答案 0 :(得分:2)

ColdFusion(*)与PHP不同......在查询字符串上的URL参数名称中添加[]并不会使它成为服务器上的数组;你的的得到的是一个逗号分隔值,然后你就可以转换为数组列表:URL.listItem.listToArray()(即' S的ColdFusion 11 ......你不要&#39 ;吨说哪个CF你&#39的版本;再利用:确保的总是的规定,在你的问题,你可以再通过与环.each().map() /。 .reduce()等等,取决于你想要对你的数组做什么。

如果一个正在寻找的所有上的URL中的参数,然后他们'重新露出的结构,所以就可以通过环的那些与该结构相当于.each()(等)方法。

(*)ColdFusion不是,但我认为是Railo和Lucee。或者有一个设置让他们表现得那样(你需要自己检查一下)。

答案 1 :(得分:2)

ColdFusion不会将键中的尾随方括号视为数组。而是像

这样的东西

listItem[]=1&listItem[]=3&listItem[]=4&listItem[]=2

将存储为:

URL["listItem[]"] = "1,3,4,2"

无论如何要实现所需的行为,您可以构建自己的URL范围。因此,您无需使用URL访问URL["listItem[]"]范围并且必须处理列表(字符串),而是可以<cfset GET = transformQuery()>使用GET访问GET["listItem"]变量检索数组。

<cffunction name="transformQuery" access="public" output="false" returnType="struct">

    <cfargument name="queryString" type="string" default="#CGI.QUERY_STRING#" required="true">

    <cfset LOCAL.result = {}>
    <!--- OR to preserve key order (becomes case sensitive!)
        <cfset LOCAL.result = createObject("java", "java.util.LinkedHashMap").init()>
    --->

    <cfset LOCAL.buffer = listToArray(ARGUMENTS.queryString, "&")>
    <cfloop array="#LOCAL.buffer#" index="LOCAL.kvp">

        <cfset LOCAL.key    = urlDecode(getToken(LOCAL.kvp, 1, "="))>
        <cfset LOCAL.value  = urlDecode(getToken(LOCAL.kvp, 2, "="))>

        <cfif reFind("\[[ ]*\]$", LOCAL.key)>

            <cfset LOCAL.key = reReplace(LOCAL.key, "\[[\s]*\]$", "")>

            <cfif structKeyExists(LOCAL.result, LOCAL.key)>

                <cfif isArray(LOCAL.result[LOCAL.key])>
                    <cfset LOCAL.result[LOCAL.key].add(LOCAL.value)>
                <cfelse>

                    <cfset LOCAL.tempBuffer = LOCAL.result[LOCAL.key]>

                    <cfset LOCAL.result[LOCAL.key] = []>
                    <cfset LOCAL.result[LOCAL.key].add(LOCAL.tempBuffer)>
                    <cfset LOCAL.result[LOCAL.key].add(LOCAL.value)>

                </cfif>

            <cfelse>

                <cfset LOCAL.result[LOCAL.key] = []>
                <cfset LOCAL.result[LOCAL.key].add(LOCAL.value)>

            </cfif>

        <cfelse>

            <cfif structKeyExists(LOCAL.result, LOCAL.key)>

                <cfif isArray(LOCAL.result[LOCAL.key])>
                    <cfset LOCAL.result[LOCAL.key].add(LOCAL.value)>
                <cfelse>

                    <!--- how to deal with keys that appear more than once, but do not contain the square bracket notation --->

                    <!--- overwrite value --->
                    <cfset LOCAL.result[LOCAL.key] = LOCAL.value>

                    <!--- OR append value (list)
                        <cfset LOCAL.result[LOCAL.key] = listAppend(LOCAL.result[LOCAL.key], LOCAL.value)>
                    --->

                    <!--- OR append value (array)
                        <cfset LOCAL.result[LOCAL.key] = [ LOCAL.result[LOCAL.key] ]>
                        <cfset LOCAL.result[LOCAL.key].add(LOCAL.value)>
                    --->

                </cfif>

            <cfelse>
                <cfset LOCAL.result[LOCAL.key] = LOCAL.value>
            </cfif>

        </cfif>

    </cfloop>

    <cfreturn LOCAL.result>
</cffunction>