By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,972 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,972 IT Pros & Developers. It's quick & easy.

asp random image plus alt description

100+
P: 229
Hi, I have this nice code that returns a random image database record. It works great. What I am trying to do now is to be able to get the "alt" description for the image from another field. If I add another field to the query it returns two images. I was making the alt alt=""" & recordset(i) & """ but I know that must be wrong. well it works but returns the image name, ie images/friday.jpg. Anyone know how I can get it to return the image description from another field related to the image randomly returned. Thanks for any help.
Richard

Expand|Select|Wrap|Line Numbers
  1.  
  2. <% 
  3. 'declare your variables
  4. 'declare SQL statement that will query your database
  5. sql = "Select thumbnailurl FROM tblGreetPost Where CategoryID <> " & 60 & " and CategoryID <> " & 63 & " and CategoryID <> " & 61 & " and CategoryID <> " & 64 & " and CategoryID <> " & 66 & " and CategoryID <> " & 65 & " ORDER BY dateadded asc"
  6. 'create ADO connection and recordset object
  7. Set connection = Server.CreateObject("ADODB.Connection")
  8. Set recordset = Server.CreateObject("ADODB.Recordset") 
  9. 'define the connection string, specify database
  10. 'driver and the location of database
  11. sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
  12. "Data Source=" & Server.MapPath("/fpdb/greetingcardpro.mdb") 
  13. 'Open the connection to the database
  14. connection.Open(sConnString)
  15. 'Open the recordset object executing the SQL 
  16. recordset.Open sql, connection, 3, 1
  17. 'count the number of records and hold this is the variable intTotalRecords
  18. intTotalRecords = recordset.RecordCount
  19. Randomize()
  20. intRandomNumber = Int(intTotalRecords * Rnd)
  21. 'move to the random number returned
  22. recordset.Move intRandomNumber
  23. 'open the table
  24. 'loop through the total number of fields
  25. For i = 0 to recordset.Fields.Count - 1
  26. 'write out the field value
  27. Response.write("<a href="""" title="" ""><img width=""100%"" class=""bord"" alt="" "" src=""" & recordset(i) & """/></a>")
  28. Next
  29. 'close the recordset and connection objects and free up resources
  30. recordset.Close
  31. Set recordset=Nothing
  32. connection.close
  33. Set connection=Nothing
  34. %>
  35.  
Jan 22 '09 #1
Share this Question
Share on Google+
3 Replies


stepterr
100+
P: 157
fran7, without digging in to deep something caught my eye. Look at one of the loops you are doing.

Expand|Select|Wrap|Line Numbers
  1.  
  2. 'loop through the total number of fields 
  3. For i = 0 to recordset.Fields.Count - 1 
  4.  'write out the field value 
  5.  Response.write ("<a href="""" title="" ""><img width=""100%"" class=""bord"" alt="" "" src=""" & recordset (i) & """/></a>") 
  6. Next 
  7.  
It looks like you are looping through each field, but yet in your query you only select one field from the table. I would start there.
Jan 22 '09 #2

jhardman
Expert 2.5K+
P: 3,405
Richard,

stepterr is right, you are currently only asking the db to give you one field of data. You will need to select two fields (something like this):
Expand|Select|Wrap|Line Numbers
  1.  
  2. <% 
  3. 'declare your variables
  4. 'declare SQL statement that will query your database
  5. sql = "Select thumbnailurl, thumbnaildescription FROM tblGreetPost Where CategoryID <> " & 60 & " and CategoryID <> " & 63 & " and CategoryID <> " & 61 & " and CategoryID <> " & 64 & " and CategoryID <> " & 66 & " and CategoryID <> " & 65 & " ORDER BY dateadded asc"
  6.  
Jared
Jan 22 '09 #3

100+
P: 229
Thanks guys, Yes that was the problem but I eventually got it going like this.
Thanks Richard


<%
' ADO Constant. Dont change this
' Connection string and SQL statement
Dim sql , connStr
sql = "Select author,artist,tip,website,thumbnailurl,postcardid, download FROM tblGreeting Where CategoryID <> " & 60 & " and CategoryID <> " & 63 & " and CategoryID <> " & 61 & " and CategoryID <> " & 64 & " and CategoryID <> " & 66 & " and CategoryID <> " & 65 & " ORDER BY dateadded asc"
connStr = " Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("/fpdb/data.mdb" )
' Opening database
Dim rs
Set rs = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, connStr, 3, , adCmdText
' Generating random number from total number of records
Dim intRnd
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))
' Now moving the cursor to random record number
rs.Move intRnd
' Showing the random statement
Response.Write " <a target=""_blank"" href=""http://""><img width=""100%"" alt=""" & rs("download") & """ border=""0"" src=""" & rs("thumbnailurl") & """></a><br><br>"
' Closing the database
rs.Close
Set rs = Nothing
%>
Jan 23 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.