Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Splitting Array

0.00/5 (No votes)
23 Aug 2004 1  
This code will help to store multiple checkbox or radio box values via Form (to render on screen / to store in a database).

Introduction

This code will help you to store checkbox or radio button values via Form (to render on screen / to store in a database). This is very useful if you have many checkboxes/radio buttons in a FORM, with the same name and passing back to the FORM. Or if you want to add all values of a variable in the database in one data field.

This code will show you how to use SPLIT() function of ASP.

SPLIT() function is useful when you are going to hold many items in just one variable. Then you can simply break that variable by SPLIT() function, to build an array. See the example code:

For any questions, feel free to ask me.

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

%>

How can we benefit from the above example?

We can set this variable to our Request.QueryString or Request.Form:

'example

'myVariable = "myValue1, myValue2, myValue3"

Using code as QueryString:

myVariable = Request.QueryString ("itemName")

Or using code as Form collection:

myVariable = Request.Form ("itemName")

If request is coming with different values but with same variable, this is where this script helps out...

Now, we should create a 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>

Got the point? After pressing Submit, the form will send out hobby as given below:

mypage.asp?hobby=martial arts&hobby=programming&hobby=books

So, do we need to create three variables to catch three values of Request? Of course! not. So, we will catch all in just one variable:

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

Now, how can we extract text from one variable? Here comes the solution: easily by SPLIT() function. Make an array to store and break the variable.

'[Split] function do that work

hobby = Split(Request.QueryString("hobby"), ",") 

SPLIT function takes two arguments:

  • SPLIT (variableName, by which character we need to break the variable)

So, we just pass the querystring and then we place ",". SPLIT() will separate the variable into an Array by cutting out ",".

Variable changed into Array as:

hobby(0) = "martial arts"
hobby(1) = "programming"
hobby(2) = "books"

Now, there is no problem to print out these values:

For i = LBound(hobby) TO UBound(hobby)
   Response.Write hobby(i) & "<BR />"
Next

And further more, you can add values in a database easily, hope so.

Have fun :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here