Forums

This topic is locked

Dynamic Array Values from a DB

Posted 03 Oct 2005 09:24:24
1
has voted
03 Oct 2005 09:24:24 Al Patel posted:
I need to create an array and assign values to each element from a database.

Right now my code is like this:

var Pic = new Array()
Pic[0] = 'gears.jpg'
Pic[1] = 'wave2.jpg'
Pic[2] = 'wave3.jpg'
Pic[3] = 'wave4.jpg'
Pic[4] = 'careers.jpg'

And I would like to assign values in some manner like this from the current row from the database that the user has selected.
Pic[0] = currentrow field pic1
Pic[1] = currentrow field pic2
Pic[2] = currentrow field pic3
Pic[3] = currentrow field pic4
... and so on...

I basically need to have my array store values from a database as opposed to hard-coding the values. Thank you...

al

Replies

Replied 04 Oct 2005 16:09:37
04 Oct 2005 16:09:37 Lee Diggins replied:
Hi Al

A rapid way would be to use the GetRows() method

Dim myArray
myArray = rsMyData.GetRows(-1)

Only problem with that is it produces a multi-dimensional array not a single and can be a pain if you're not used to working with them, but can be accessed like this:

For i = 0 to UBound(myArray, 2)
Response.Write(myArray(0, i) & "<BR>"
i = i + 1
Next

Read about this method here:
www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=161

The other way would be to use a while loop as in a repeat region:

Dim myArray, i

i = 0

Do While Not rsMyData.BOF Or Not rsMyData.EOF

myArray(i) = rsMyData.Fields.item("myColumn".Value

i = i + 1

rsMyData/MoveNext()

Loop

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>
Replied 04 Oct 2005 17:41:38
04 Oct 2005 17:41:38 Al Patel replied:
Lee, thanks for the help. I was thinking of a solution similar to your second suggestion. Along that line, I will have only 1 row of data with up to 10 fields of data that I want to store in an array. Your solution flips through rows of data in the recordset as opposed to fields right?

My database table will store info on cars, ie:

Make
Model
Year
Pic1
Pic2
Pic3
Pic4
...
Pic10

I need the Pic1 - Pic10 values to be stored in the array, all from the same row in the DB. I hope I am making sense?

al
Replied 04 Oct 2005 18:28:44
04 Oct 2005 18:28:44 Lee Diggins replied:
Hi Al

I would use the GetRows() method, have another look at the example in the link I posted previously and look for this code where you can specify the filed to include:

ArrayName = objRS.GetRows(, , Array("ColumnName1", "ColumnName2", ...))

Give it a go, if you get stuck post back.

Sharing Knowledge Saves Valuable Time!!!
~ ~ ~ ~ ~ ~
<b>Lee Diggins</b> - <i>DMXzone Manager</i>
<font size="1">[ Studio MX/MX2004 | ASP -> VBScript/PerlScript/JavaScript | SQL | CSS ]</font>

Reply to this topic