Click here to Skip to main content
15,867,308 members
Articles / Web Development / HTML
Article

Splitting Array

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
23 Aug 20042 min read 90.9K   16   7
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

VBScript
<%
'First declare a variable [myVariable]
'It contains 3 values each of these are seperated
'by a comma , (you may use any seperator)
VBScript
myVariable = "myValue1, myValue2, myValue3"
VBScript
'Make an array to store and break the variable
'[Split] function do that work
VBScript
myArray = Split(myVariable,",")
VBScript
'now we can see logically our Variable have been broken 
'into 3 parts as an Array 
'something like: 
'*************** 
'Logical Diagram 
'***************
VBScript
'  ------------------------
'  |  myArray   | Value    |
'  ------------------------
'  |    0       | myValue1 |
'  |    1       | myValue2 |
'  |    2       | myValue3 |
'  ------------------------
VBScript
'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
VBScript
For i = LBound(myArray) TO UBound(myArray)
   Response.Write myArray(i) & "<BR />"
Next
VBScript
'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...
VBScript
'First Time Loop (view logical diagram)
-------------
'For i = 0
   'Response.Write myArray(0) it's equal to [myValue1]
'Next
-------------
VBScript
'Second Time Loop (view logical diagram)
-------------
'For i = 1
   'Response.Write myArray(1) it's equal to [myValue2]
'Next
------------- 
VBScript
'Third Time Loop (view logical diagram)
-------------
'For i = 2
   'Response.Write myArray(2) it's equal to [myValue3]
'Next
------------- 
VBScript
'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:

VBScript
'example
'myVariable = "myValue1, myValue2, myValue3"

Using code as QueryString:

VBScript
myVariable = Request.QueryString ("itemName")

Or using code as Form collection:

VBScript
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:

HTML
<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:

VBScript
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.

VBScript
'[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:

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

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

VBScript
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


Written By
Web Developer
Singapore Singapore
ALLAH’ the most Beneficent, ever Merciful Lord of the Universe
------------
An experienced web developer having self developed various kind of projects.

My areas of computing are:
Hardware & Networks, Systems, Languages, Databases, Graphics, Web Development

Always keep looking for knowledge lovers, who like to spread knowledge, So if you are one of such people join at www.coislamscience.com knowledge site.

Comments/Suggestions are always welcome!

Comments and Discussions

 
Generali am unable to split date object Pin
LIBIN P A10-May-08 1:51
LIBIN P A10-May-08 1:51 
QuestionPlease Help Pin
rafusextreem31-Jan-07 3:09
rafusextreem31-Jan-07 3:09 
GeneralRe: Great article for beginner Pin
Muhammad Zeeshan Arshad3-Sep-04 2:23
Muhammad Zeeshan Arshad3-Sep-04 2:23 
GeneralRe: Great article for beginner Pin
AnaiAnai17-Oct-07 21:52
AnaiAnai17-Oct-07 21:52 
QuestionA simpler way? Pin
RubensFarias2-Sep-04 11:20
RubensFarias2-Sep-04 11:20 
AnswerRe: A simpler way? Pin
Muhammad Zeeshan Arshad3-Sep-04 2:17
Muhammad Zeeshan Arshad3-Sep-04 2:17 
GeneralGreat article for beginner Pin
aprenot2-Sep-04 4:10
aprenot2-Sep-04 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.