ASP classic - how do I join an array of strings / join / implode do not work

陌路散爱 提交于 2019-12-11 02:16:31

问题


Dim stringIdCollection
    stringIdCollection = Join( Request.Form( "id" ), "', '" ) )    
Dim whereStatement
    whereStatement = "WHERE id IN ('" & stringIdCollection & "');"

I get this error:

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/includes/Process.asp, line 49 stringIdCollection = Join( Request.Form( "id" ), "', '" ) ) ------------------------------------------------------------------------^

Is it even possible to use Join on a Request.Form?

I would like the output to be:

"WHERE id IN ('122', '344', '599')"


回答1:


The join function expects an array as the first parameter. In Classic ASP (VBScript) the data in Request.Form is always string, so impossible to have an actual array in it, you have to build the array yourself :

Dim myArray
myArray = Array(Request.Form("id"), Request.Form("id2"), Request.Form("id3"))

Dim stringIdCollection
stringIdCollection = Join( myArray , "', '" ) ) 

Also notice that in Classic ASP, if you submit multiple form fields with the same names (i.e. id), they will get in Request.Form already separated by a commas.

Contrary to what can be done in PHP, naming multiple form fields with square brackets [] appended at the end, ASP will not convert it as an array in Request.Form.




回答2:


That looks like an extra parentheses on the second line that should be deleted:

    stringIdCollection = Join( Request.Form( "id" ), "', '" )

EDIT: The documentation for Request.Form is a little bit misleading. The value returned by Request.Form("item") isn't really an array, but an array-like object. You can iterate through those values by using the Count property and build your string as you iterate:

Dim stringIdCollection, i

For i = 1 To Request.Form("id").Count
    If Len(stringIdCollection) <> 0 Then
        stringIdCollection = stringIdCollection & ","
    End If
    stringIdCollection = stringIdCollection & "'" & Request.Form("id")(i) & "'"
Next


来源:https://stackoverflow.com/questions/15113216/asp-classic-how-do-i-join-an-array-of-strings-join-implode-do-not-work

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