Dynamic array creation

hiya
i am having a problem purely creating arrays dynamically.
what i want to do is count the matches from the database (for example 4)
then create 4 arrays and populate them with the information from the database.
I have managed to dynamically create arrays using asp and javascript, but is there a way to only use asp to create this?
Current code below:-

<%
Dim sSQL     'SQL String for Product Return '
Dim cSQL     'SQL Count for total number of items '
Dim RS     'Needed to fire off the Count items'
Dim ReturnCount     'Used to create the javascript arrays  '
Dim i     'Incrementer to count the total number '
Dim metal     'Database value  '
Dim RecordNum     '  '
Dim ArrayNum     '  '

RecordNum ="1"
ArrayNum ="1"
sSQL = "SELECT * FROM baby WHERE PRODUCT ='rabbit'"
Set objRS = objConn.Execute(sSQL)

cSQL = "SELECT COUNT(*) FROM baby WHERE PRODUCT ='rabbit'"
'AS Total_Page_Hits sets the alias'
Set RS = objConn.Execute(cSQL)

Set ReturnCount = RS(0)


' array creation'
Dim aray
aray = "record"
Dim ci
ci = 1

%>
<script LANGUAGE='JavaScript'>
<%

For i = 1 to RS(0)

dim master          'to add both record and number to create individual arrays'
master = Aray & ci

%>
var <%=master%>;
<%=master%> = new Array();

<%
ci = ci + 1
next


Do While NOT objRS.EOF
for each metal in objRS.fields

%>
record<%=RecordNum%>[<%=ArrayNum%>] = "<%=metal.value%>";
<%

ArrayNum = ArrayNum + 1
next


objRS.MoveNext
ArrayNum = 1
RecordNum = RecordNum + 1
Loop
%>
</script>
<TR>

<%

Dim m
Dim NewNum
Dim qa


NewNum = 1
m = "2"
oddEven = ""

Set objRS = objConn.Execute(sSQL)



Do While qa < ReturnCount
for qa = NewNum to ReturnCount



if(m mod 2 = 0) then

          response.write("<TABLE border='0' cellspacing='0' cellpadding='8'  width='266'><TR VALIGN='top'><TD><img src='../images/<script>record"  & NewNum & "[6]);</script>.jpg' </TD>")

else

          response.write("<TABLE border='0' cellspacing='0' cellpadding='8'  width='266'><TR VALIGN='top'><TD><img src='../images/<script>record"  & NewNum & "[6]);</script>.jpg' </TD>")

end if
m = m +1
NewNum = NewNum +1

next
objRS.MoveNext

Loop
%>

cheers!
rednose
rednosecodeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markhoyCommented:
i'm not sure if this helps to but to dynamically create arrays try this:

dim myArray()
x=100 ' or any value you create
redim preserve myArray(x) ' use preserve to keep contents of array, as needed
0
rednosecodeAuthor Commented:
i thinkl that will only allow me to resize an array.
what i am after is to say create 4 arrays (dynamically) and populate them individually. so :-
array1
array2
array3
array4

array1 = cat, mat, sat, dog
array2 = cat1, mat2, sat1, dog1
etc..
the main thing here is to create the names of the array's dynamically and then be able to refererence them. appologies if was a bit vague :)
0
hannemanCommented:
Im not exactly sure what you mean. Are you trying to dynamically declare the array name as well?
Im not sure if you can do this, here is an idea or 2:

write out the array declarations to a file using the file system object, then use server.execute("thefilename") to create the arrays.

or you could have just one array, but multidimensional, put the master name in the first element of the array:

myarray(0,0) = "master"
myarray(1,0) = "a value"
myarray(2,0) = "another value"

myarray(0,1) = "another master"
myarray(1,1) = "a value"
myarray(2,1) = "another value"

-h



0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

markhoyCommented:
somthing like

x =4

for i =0 to x
dim myArray_& i & ()
next

haven't tried it but you can use & i & to dynamically read request.form fields so it may work with arrays

ie

for i = 0 to 5
myvar= request.form("myvar"&i)
next
will populate an array called myvar with 6 values

so this might work

for i = 0 to 5
myvar&i= request.form("myvar"&i)
next



0
hannemanCommented:
this will declare anynumber of arrays, and populate them too with some small changes.

filename = server.mappath("testfile.txt")
 
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set tso = fso.OpenTextFile(Filename, 2, True)

tso.write(chr(60) & chr(37))
counter = 0
do while not counter = 5


tso.write("myarray" & counter & " = array() " & chr(13) & chr(10))
 
counter = counter + 1
loop

tso.write(chr(37) & chr(62))

tso.close
 
server.execute("testfile.txt")

0
sybeCommented:
Or use a dictionary object, you'll have all your arrays sticked to a single object, but the "names" can be generated dynamically.


<%
Dim ArrayDictionary, i
Set ArrayDictionary = Server.CreateObject("Scripting.Dictionary")


For i = 1 To 10
    ArrayDictionary("array_nr_" & i) = Array("a","b","c")
Next
%>


0
hannemanCommented:
true. dictionary is nice and tidy.
0
keystrokesCommented:
Why would you use so many arrays?
0
rednosecodeAuthor Commented:
sybe - could you explain more please.
as I understand it you have created 10 arrays
called
array_nr_1
array_nr_2
array_nr_3 (through to 10..)
then populated each array with the info a,b, c

how would i reference the arrays?
I receive type mismatch errors when trying to run the page
cheers,

0
rednosecodeAuthor Commented:
i want to create arrays for each row that is pulled from the database - each row has seven individual pieces of info in it. I want to do this so I can manipulte the data and reuse it on different pages- rather than having to call it from the database each time as there may be many row returns - something that i cannot predict.
The multidimensional array would seem a good way - but i cannot seem to use variables to increment the referenceing part i.e
array(incrementingNumberVariable1, incrementingNumberVariable2) = "dog"

array(incrementingNumberVariable1, incrementingNumberVariable2) = "cat"

which would in fact be ;-
array(0,1) = " dog"
array(0,2) = "cat"


cheers
0
sybeCommented:
Well, having a closer look at your code, i am a bit confused.

When I look at your code, at some part you want to create javascript arrays by ASP. Am I right ? This is very well do-able, but a bit tricky.

It would be helpfull to write down the javascript code you want to have as output.

0
sybeCommented:
Have you considered (this is pure ASP) - I still am not exactly sure about what you want.

<%
MyTwoDimensionalArray = recordset.GetRows()
%>
0
hannemanCommented:
DOH! getrows is a fantastic method for creating a 2D array from a recordset. if you still want to create individual arrays, this should work: (sorry, untested)

<%
filename = server.mappath("testfile.txt")

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set tso = fso.OpenTextFile(Filename, 2, True)

tso.write(chr(60) & chr(37))
counter = 0

do while not ors.eof

tempstr = ""
for each i in ors.fields
tempstr =  tempstr & ors.fields(i) & ","
next

tempstr = left(tempstr, len(tempstr)-1)

tso.write("myarray" & counter & " = split("&tempstr&") " & chr(13) & chr(10))

counter = counter + 1
ors.movenext

tso.write(chr(37) & chr(62))

tso.close

server.execute("testfile.txt")
%>

but I would go with getrows, personally.
-h
0
rednosecodeAuthor Commented:
i have this working creating javascript arrays - but i want to create the arrays purely by asp
so for each full row an array is created containing all that rows data.
so if there are 4 rows to be returned 4 arrays are created
appologies if i havnt explained myself
cheers
0
hannemanCommented:
you said the multidimensional array was a good idea.
sybe has given you the tidiest code there is to create it in asp. (getrows())

the examples I have given should create seperate arrays if that is what you want.
0
rednosecodeAuthor Commented:
the multidimensional arrays would be good, but i cannot seems to get them to work using Dims to place the data in the arrays. I am looking at getrows - and this does seem the most efficiant way of getting the data. the problem i have is placing each row in a unique array - as i do not know at the start how many array i need to create which is why i am trying to create arrays with unique names as required. the 1st script at the top works by creating unique javascript arrays based on ASP Dims that increment.
but i have run into problems using this and wish to have the arrays created dynamically using just asp.
Please be patient i am learning as i go and you guys have given me a lot of information to absorb.
cheers,
0
sybeCommented:
GetRows() will create a 2-dimensional array with values of *all* records. So if you would have a recordset of 5 records, each with 3 fields, the result would be:

value_field1_record1 value_field2_record1 value_field3_record1
value_field1_record2 value_field2_record2 value_field3_record2
value_field1_record3 value_field2_record3 value_field3_record3
value_field1_record4 value_field2_record4 value_field3_record4
value_field1_record5 value_field2_record5 value_field3_record5
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CleanupPingCommented:
rednosecode:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
GaryCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by sybe

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.