Click here to Skip to main content
Licence 
First Posted 23 Aug 2004
Views 64,313
Bookmarked 16 times

Splitting Array

By | 23 Aug 2004 | Article
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

About the Author

Muhammad Zeeshan Arshad

Web Developer

Singapore Singapore

Member

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!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generali am unable to split date object PinmemberLIBIN P A1:51 10 May '08  
QuestionPlease Help Pinmemberrafusextreem3:09 31 Jan '07  
GeneralRe: Great article for beginner PinmemberZeeshan Arshad2:23 3 Sep '04  
GeneralRe: Great article for beginner PinmemberAnaiAnai21:52 17 Oct '07  
QuestionA simpler way? PinmemberRubensFarias11:20 2 Sep '04  
AnswerRe: A simpler way? PinmemberZeeshan Arshad2:17 3 Sep '04  
GeneralGreat article for beginner Pinmemberaprenot4:10 2 Sep '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Aug 2004
Article Copyright 2004 by Muhammad Zeeshan Arshad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid