While passing single dimension array can be done through string passing, passing the multidimensional array is not that straight forward. A single string cannot not hold multidimensional array element. breaking the array into 2 or more separate strings, though able to pass the value, will not ensure the matching of the array elements to the correct pair.
I assume that many of us know that we can pass array to another asp page using session variable. I would rather find another method than passing any value into a session variable if if i have a choice. Why? Session variable is ASP is equivalent to global variable in visual basic. You can store anything in session variable. From a simple string to a large ADO object if you store a value in session variable and the value is huge it will affect the performance of your website. But are session variables really evil? You can read more about Pros and cons of session variable.
I assume that many of us know that we can pass array to another asp page using session variable. I would rather find another method than passing any value into a session variable if if i have a choice. Why? Session variable is ASP is equivalent to global variable in visual basic. You can store anything in session variable. From a simple string to a large ADO object if you store a value in session variable and the value is huge it will affect the performance of your website. But are session variables really evil? You can read more about Pros and cons of session variable.
Method 1: Passing through a Session Variable
Still , the simplest way to pass the multidimensional array to another page is by using session variable, The codes below show how you can do it.
you have created and populate a multidimensional array in page 1 and you want to pass it to page 2
Page 1
1. create the array
dim arrtest(3,2)
2. Then you populate your array
arrtest(0,0)="123"
arrtest(0,1)="Hen"
arrtest(0,1)="Hen"
arrtest(0,2)="Chicken"
arrtest(1,0)="123"
arrtest(1,0)="123"
arrtest(1,1)="Cat"
arrtest(1,2)="Kitten"
3. Pass it into a session variable. Name is not necessary similar but a meaningful name will make you easy.
session("arrtest")=arrtest
Page 2
1.Extract the array
dim arrtestpass
arrtestpass=session("arrtest")
2. Display the content
for i=0 to ubound(arrtestpass,2)
for j=0 to ubound(arrtestpass,1)
arrtestpass(i.j)
next
next
2. Display the content
for i=0 to ubound(arrtestpass,2)
for j=0 to ubound(arrtestpass,1)
arrtestpass(i.j)
next
next
arrtestpass is now an array and can be used in page 2
Method 2: Store in Temporary Database Table
This method is ideal if your array store large data or object such as big size picture.
Page 1
Insert the multidimensional array into a temporary table.The table arrtest_tab need to be created first it it still not exist
CREATE TABLE arrtest_tab (test_id number animal text, offspring text)
for i=0 to ubound(arrtest,2)
strinsert='INSERT into arrtest_tab(test_id,animal,offspring) values("
for j=0 to ubound(arrtest,1)
strinsert=strinsert & arrtest(i,j)
if i=ubound(arrtest,2) then strinsert=strinsert & "')" else strinsert=strinsert &"',"
next
next
if strinsert<>"" then cn.execute(strinsert)
Page 2
Display and delete the temporary record
'if store in database table, query and display
strcart="select item_name, item_qty from acart_tab"
set rscart=cn.execute(strcart)
if not rscart.eof then
acart=rscart.getrows()
end if
for n=0 to ubound(acart,2)
Method 3: Chunk to Separate String by Column
Page 1:
stritem=""
strqty=""
for icart=0 to ubound(acart,2)
for jcart=0 to ubound(acart,1)
if jcart=0 then stritem=stritem & acart(jcart,icart)& ","
if jcart=1 then strqty=strqty & acart(jcart,icart) &","
next
next
stritem=left(stritem,len(stritem)-1)
strqty=left(strqty,len(strqty)-1)
response.redirect "order_checkout.asp?stritem="&stritem&"&strqty="&strqty
Method 2: Store in Temporary Database Table
This method is ideal if your array store large data or object such as big size picture.
Page 1
Insert the multidimensional array into a temporary table.The table arrtest_tab need to be created first it it still not exist
CREATE TABLE arrtest_tab (test_id number animal text, offspring text)
for i=0 to ubound(arrtest,2)
strinsert='INSERT into arrtest_tab(test_id,animal,offspring) values("
for j=0 to ubound(arrtest,1)
strinsert=strinsert & arrtest(i,j)
if i=ubound(arrtest,2) then strinsert=strinsert & "')" else strinsert=strinsert &"',"
next
next
if strinsert<>"" then cn.execute(strinsert)
Page 2
Display and delete the temporary record
'if store in database table, query and display
strcart="select item_name, item_qty from acart_tab"
set rscart=cn.execute(strcart)
if not rscart.eof then
acart=rscart.getrows()
end if
To display
for n=0 to ubound(acart,2)
for m=0 to ubound(acart,1)
response.Write acart(m,n)
response.Write "|"
next
response.Write "P"
next
Delete the temporary record from the database
strdelete="TRUNCATE table acart_tab"
cn.execute(strdelete)
cn.execute(strdelete)
Method 3: Chunk to Separate String by Column
Page 1:
stritem=""
strqty=""
for icart=0 to ubound(acart,2)
for jcart=0 to ubound(acart,1)
if jcart=0 then stritem=stritem & acart(jcart,icart)& ","
if jcart=1 then strqty=strqty & acart(jcart,icart) &","
next
next
stritem=left(stritem,len(stritem)-1)
strqty=left(strqty,len(strqty)-1)
response.redirect "order_checkout.asp?stritem="&stritem&"&strqty="&strqty
Page 2
'store in multiple string
redim acart(1,0)
aitem=split(request.QueryString("stritem"),",")
aqty=split(request.QueryString("strqty"),",")
for itemcount=0 to ubound(aitem)
redim preserve acart(1,itemcount)
acart(0,itemcount)=aitem(itemcount)
acart(1,itemcount)=aqty(itemcount)
next
To display
for n=0 to ubound(acart,2)
for m=0 to ubound(acart,1)
response.Write acart(m,n)
response.Write "|"
next
response.Write "P"
next
I AM THE AUTHOR
No comments:
Post a Comment