Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create dynamic array from rs 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
0
0
GB
hi,

currently use the following code to create an array
which is fine, but have probs when field has no value.

have the following fields in a table
photo1
photo2
photo3

the following code gets these fields
and puts them into an array.
this array is then used by a flash slideshow to display the images.
the problem starts when one of the pictures is missing
and the array has still been filled by three records, one of which is blank.
i need to build the array dynamically and only include
fields in the array that have value.

merry xmas to all

Code:
photosql = "SELECT stockid, photo1, photo2, photo3 "_
& "FROM stock "_
& "WHERE stockid = 1"
Set rsphoto = Server.CreateObject("ADODB.Recordset")
rsphoto.Open photosql, conn
x_photo1 = rsphoto("photo1")
x_photo2 = rsphoto("photo2")
x_photo3 = rsphoto("photo3")
rsphoto.close
Set rsphoto = Nothing

 Dim aImages 
   ReDim aImages(2)
   aImages(0) = x_photo1 
   aImages(1) = x_photo2 
   aImages(2) = x_photo3
 
Interesting problem, there are quite a few ways to approach it. Here's my favorite for a first try:
Code:
photosql = "SELECT stockid, photo1, photo2, photo3 "_
& "FROM stock "_
& "WHERE stockid = 1"
Set rsphoto = Server.CreateObject("ADODB.Recordset")
rsphoto.Open photosql, conn
[COLOR=red]
x_photos = ""
c=0
For i=1 to 3
   If rsphoto("photo" & i)<>"" Then
      If c>0 Then x_photos = x_photos & "," End If
      x_photos = x_photos & rsphoto("photo" & i)
      c = c+1
   End If
Next
aImages = Split(x_photos,",")
[/color]
rsphoto.close
Set rsphoto = Nothing
Untested, but I hope that will work for you. If all goes well aImages will be an array of just the existing photos. A disclaimer is that I don't know how your images are coming out of the database, you might need to use If Not IsNull(rsphoto("photo" & i)) if they come through as null items rather than blank fields. Good luck, hope this helps!
 
What about keeping it the same apart from changing

x_photo1 = rsphoto("photo1")
x_photo2 = rsphoto("photo2")
x_photo3 = rsphoto("photo3")

to

x_photo1 = rsphoto("photo1") & ""
x_photo2 = rsphoto("photo2") & ""
x_photo3 = rsphoto("photo3") & ""

So if it is a null value it won't cause problems?
 
hi ecwcee,

thanks for your reply
have not ot your solution to work yet - but am still trying
just to clarify, image paths are coming from database
eg. rsphoto("photo1") is ../stock/3.jpg

and my blank ones are nulls

a question how does the array get built with one line

Code:
aImages = Split(x_photos,",")

surely it needs to be in the loop?

thanks again
 
It doesn't need to be in the loop. What the For loop I wrote does is concatenate all of the image paths that aren't null/blank into a string, with the paths separated by commas. The line:
Code:
aImages = Split(x_photos,",")
Splits the string x_photos into an array. Every element in the string that is separated by a comma gets put into its own slot in an array, so aImages will work exactly as you have intended and the slots in the array will only be populated with existing images. If you post some errors I can help you through why my code might not be working, it's probably the IsNull comment I made in my first post that needs to be added to the condition. Here's how you would access the elements of the aImages array:
Code:
For i=0 to UBound(aImages)
   'However you display image aImage(i)
Next
 
v clever - have a star
am learning about arrays all the time. When i first used them i thought whats the point, i've got sql and tables!
but i can see how useful they can be

many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top