分割数组






3.67/5 (2投票s)
2004年8月24日
2分钟阅读

92125
这段代码将帮助你通过表单存储多个复选框或单选按钮的值(用于在屏幕上渲染/存储在数据库中)。
引言
这段代码将帮助你通过表单存储复选框或单选按钮的值(用于在屏幕上渲染/存储在数据库中)。如果你在表单中有很多具有相同名称的复选框/单选按钮,并将它们传递回表单,这将非常有用。或者,如果你想将变量的所有值在一个数据字段中添加到数据库中。
这段代码将展示如何使用 ASP 的 SPLIT()
函数。
SPLIT()
函数在你需要在单个变量中保存多个项目时非常有用。然后,你可以简单地使用 SPLIT()
函数来分解该变量,从而构建一个数组。请参阅示例代码
如有任何问题,请随时向我提问。
Using the Code
<%
'First declare a variable [myVariable]
'It contains 3 values each of these are seperated
'by a comma , (you may use any seperator)
myVariable = "myValue1, myValue2, myValue3"
'Make an array to store and break the variable
'[Split] function do that work
myArray = Split(myVariable,",")
'now we can see logically our Variable have been broken
'into 3 parts as an Array
'something like:
'***************
'Logical Diagram
'***************
' ------------------------
' | myArray | Value |
' ------------------------
' | 0 | myValue1 |
' | 1 | myValue2 |
' | 2 | myValue3 |
' ------------------------
'Split function brokes variable and makes Array
'Array always start from 0 (zero) to last number
'Now we need a loop to render array on screen
For i = LBound(myArray) TO UBound(myArray)
Response.Write myArray(i) & "<BR />"
Next
'run this code you will see the result
'what going on to the above line?
'See the following example.
Loop was running 3 times, each times value for i= changed, read below...
'First Time Loop (view logical diagram)
-------------
'For i = 0
'Response.Write myArray(0) it's equal to [myValue1]
'Next
-------------
'Second Time Loop (view logical diagram)
-------------
'For i = 1
'Response.Write myArray(1) it's equal to [myValue2]
'Next
-------------
'Third Time Loop (view logical diagram)
-------------
'For i = 2
'Response.Write myArray(2) it's equal to [myValue3]
'Next
-------------
'so it will loop thrice and array values will be print out on the screen
%>
我们如何从上面的示例中受益?
我们可以将此变量设置为我们的 Request.QueryString
或 Request.Form
'example
'myVariable = "myValue1, myValue2, myValue3"
将代码用作 QueryString
myVariable = Request.QueryString ("itemName")
或将代码用作 Form
集合
myVariable = Request.Form ("itemName")
如果请求使用不同的值但具有相同的变量,那么这个脚本就能派上用场……
现在,我们应该创建一个 Form
<form action="mypage.asp">
my hobbies:
<input type="radio" name="hobby" value="martial arts" /> Martial Arts <br />
<input type="radio" name="hobby" value="programming" /> Programming <br />
<input type="radio" name="hobby" value="books" /> books <br />
<input type="submit" value="Go!" />
</form>
理解了吗?按下提交后,表单将发送如下爱好:
mypage.asp?hobby=martial arts&hobby=programming&hobby=books
所以,我们需要创建三个变量来捕获 Request 的三个值吗?当然不需要。所以,我们将全部捕获到一个变量中
Dim hobby
hobby = Request.QueryString ("hobby")
'now, if we will print above variable on the screen it will display the result as:
Response.Write hobby
'Screen Result will be as =>>> martialarts,programming,books
现在,我们如何从一个变量中提取文本?SPLIT()
函数就是解决方案:轻松地使用 SPLIT()
函数。创建一个数组来存储和分解变量。
'[Split] function do that work
hobby = Split(Request.QueryString("hobby"), ",")
SPLIT
函数接受两个参数
- SPLIT (变量名,用哪个字符来分解变量)
所以,我们只需传递查询字符串,然后放置“,”。SPLIT()
将通过切分“,”将变量分解为数组。
变量转换为数组如下:
hobby(0) = "martial arts"
hobby(1) = "programming"
hobby(2) = "books"
现在,打印这些值不再有问题
For i = LBound(hobby) TO UBound(hobby)
Response.Write hobby(i) & "<BR />"
Next
此外,你还可以轻松地将值添加到数据库中,希望如此。
玩得开心:)