?
Solved

Dynamic array creation

Posted on 2003-02-28
20
Medium Priority
?
290 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:rednosecode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +4
20 Comments
 
LVL 7

Expert Comment

by:markhoy
ID: 8040751
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
 

Author Comment

by:rednosecode
ID: 8040845
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
 
LVL 4

Expert Comment

by:hanneman
ID: 8040863
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:markhoy
ID: 8040865
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
 
LVL 4

Expert Comment

by:hanneman
ID: 8040872
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
 
LVL 28

Expert Comment

by:sybe
ID: 8040939
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
 
LVL 4

Expert Comment

by:hanneman
ID: 8041058
true. dictionary is nice and tidy.
0
 
LVL 3

Expert Comment

by:keystrokes
ID: 8044085
Why would you use so many arrays?
0
 

Author Comment

by:rednosecode
ID: 8056821
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
 

Author Comment

by:rednosecode
ID: 8056852
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
 
LVL 28

Expert Comment

by:sybe
ID: 8056884
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
 
LVL 28

Expert Comment

by:sybe
ID: 8056899
Have you considered (this is pure ASP) - I still am not exactly sure about what you want.

<%
MyTwoDimensionalArray = recordset.GetRows()
%>
0
 
LVL 4

Expert Comment

by:hanneman
ID: 8056953
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
 

Author Comment

by:rednosecode
ID: 8056976
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
 
LVL 4

Expert Comment

by:hanneman
ID: 8057067
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
 

Author Comment

by:rednosecode
ID: 8057124
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
 
LVL 28

Accepted Solution

by:
sybe earned 200 total points
ID: 8057166
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
 

Expert Comment

by:CleanupPing
ID: 9576807
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
 
LVL 58

Expert Comment

by:Gary
ID: 9752605
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question