2011-02-03 39 views
3

當前,表單中存在複選框,並且在提交表單時,所選複選框的值將存儲在數據庫中。帶有逗號變量的ColdFusion表格數組

<td><input type="Checkbox" name="valueList" value="Some value, with comma" >Some value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Another Value, with comma" >Another value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Yet another value" >Yet another value</td> 

但是,問題是用逗號作爲當前邏輯使用列表來存儲這些值。所以Some value, with comma被插入爲Some valuewith comma。當前列表創建以下內容:

<cfif isDefined("valueList")> 
<cfset a=listlen(valueList)> 

然後代碼繼續循環通過列表。這是我能在代碼中找到的valueList的唯一參考。有沒有一種方法可以將這個轉換爲一個數組而不會產生問題?

回答

1

我使用的模式是用逗號(,)代替逗號(〜),因爲它根本不在我們的域中使用,你可以使用任何你想要的字符。

<td><input type="Checkbox" name="valueList" value="Some value~ with comma" >Some value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Another Value~ with comma" >Another value, with comma</td> 
<td><input type="Checkbox" name="valueList" value="Yet another value" >Yet another value</td> 

所以,當形式過來這將是如下:

form.valueList = "Some value~ with comma, Another Value~ with comma, Yet another value"; 

這是代碼,以獲得您想要的數組:

<cfscript> 
    variables.myArrayList = ListToArray(form.valueList); 
    for(i=1; i LTE ArrayLen(variables.myArrayList); i=i+1) 
    { 
    variables.myArrayList[i] = ReplaceNoCase(variables.myArrayList[i],"~",",","all"); 
    } 
</cfscript> 
+0

如果您不確定值或者是用戶定義的,則是波浪號同樣糟糕。我的一個同事會拿出最荒謬的分隔符,如<(|)>來處理ColdFusions愚蠢的列表。 – scragz 2012-11-30 03:22:19

1

分隔符(在此例如逗號)應該在數據中存在而不是,因爲那樣幾乎不可能識別一個元素開始而另一個結束。最好的解決方案是使用其他的東西來複選框value。例如,如果描述來自數據庫表,則使用數字記錄ID而不是長文本描述。那麼這將是一個非問題。

+0

你是對的,但假設它首先來自數據庫。仍然有很多情況需要輸入包含逗號的事物列表。 – Teekin 2011-09-22 14:17:48

9

其實一種以數組方式檢索數據的方法。如果您是enctype是application/X WWW的形式,進行了urlencoded(默認ENCTYPE),那麼你只需要一條線:

<cfset myArray = getPageContext().getRequest().getParameterValues('my_form_or_url_field_name')> 

如果你的加密類型是多部分/表單數據(這是你在上傳時所使用的類型文件),那麼事情會更復雜一些。這是一個功能我寫的,將使用相同的名字回執& URL值作爲一個數組,對於任何一個加密類型:

<cffunction name="FormFieldAsArray" returntype="array" output="false" hint="Returns a Form/URL variable as an array."> 
    <cfargument name="fieldName" required="true" type="string" hint="Name of the Form or URL field" /> 

    <cfset var tmpPartsArray = Form.getPartsArray() /> 
    <cfset var returnArray = arrayNew(1) /> 
    <cfset var tmpPart = 0 /> 
    <cfset var tmpValueArray = "" > 

    <!--- if the getPartsArray method did not return NULL, then this is a multipart/form-data request, which must be handled as such. ---> 
    <cfif IsDefined("tmpPartsArray")> 
     <cfloop array="#tmpPartsArray#" index="tmpPart"> 
      <cfif tmpPart.isParam() AND tmpPart.getName() EQ arguments.fieldName> 
       <cfset arrayAppend(returnArray, tmpPart.getStringValue()) /> 
      </cfif> 
     </cfloop> 
    </cfif> 

    <!--- Add the values that maybe on the URL with the same name, also if this *wasn't* a multipart/form-data request then 
    the above code did not get any of the data, and the method below will return all of it. ---> 
    <cfset tmpValueArray = getPageContext().getRequest().getParameterValues(arguments.fieldName) /> 

    <!--- that may have returned null, so need to test for it. ---> 
    <cfif IsDefined("tmpValueArray")> 
     <cfloop array="#tmpValueArray#" index="tmpPart"> 
      <cfset arrayAppend(returnArray, tmpPart) /> 
     </cfloop> 
    </cfif> 

    <cfreturn returnArray /> 
</cffunction>