asp display random to table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    asp display random to table

    Hi, I wonder if anyone can help. I have this asp which selects randomly from my database. works fine but writes the results to a table. One of the returned results is an image so I need to wrap it in src. Problem is it wraps all the results in src. Anyone suggest a way to write the results so that one can custom each individually?
    Any help would be great.
    Thanks
    Richard

    Code:
    <% 
    'declare your variables
    dim connection, recordset, sConnString, sql
    dim intRandomNumber, intTotalRecords, i
    
    'declare SQL statement that will query your database
    sql = "Select author, ThumbnailURL, downloadseven FROM tblgreetone where extrathree = 'yes' ORDER BY Authortwo asc"
    
    'create ADO connection and recordset object
    Set connection = Server.CreateObject("ADODB.Connection")
    Set recordset = Server.CreateObject("ADODB.Recordset") 
    
    'define the connection string, specify database
    'driver and the location of database
    sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
    "Data Source=" & Server.MapPath("/fpdb/greetingcardpro.mdb") 
    
    'Open the connection to the database
    connection.Open(sConnString)
    
    'Open the recordset object executing the SQL 
    recordset.Open sql, connection, 3, 1
    'count the number of records and hold this is the variable intTotalRecords
    intTotalRecords = recordset.RecordCount
    Randomize()
    intRandomNumber = Int(intTotalRecords * Rnd)
    'move to the random number returned
    recordset.Move intRandomNumber
    'open the table
    Response.write("<table border='0'>")
    'loop through the total number of fields
    For i = 0 to recordset.Fields.Count - 1
    'write out the field value
    Response.write("<tr><td><img src=""" & recordset(i) & """></td></tr>")
    Next
    'close the table
    response.write("</table>")
    
    'close the recordset and connection objects and free up resources
    recordset.Close
    Set recordset=Nothing
    connection.close
    Set connection=Nothing
    %>
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Richard,

    You could test whether the item retrieved has an image type file extension or not. Something like this should do the trick:

    Code:
     <% 
    If InStr(recordset(i), ".") > 0 Then
    	Select Case Split(recordset(i), ".")(1)
    	 Case "tif", "gif", "png", "jpg", "jpeg"
    			 Response.write("<tr><td><img src=""" & recordset(i) & """></td></tr>")
    	 Case Else
    			 Response.write("<tr><td>" & recordset(i) & "</td></tr>")
    	End Select
    End If
    %>
    Hopefully you can see how this example works.

    Let me know how you get on.

    Dr B

    Comment

    • fran7
      New Member
      • Jul 2006
      • 229

      #3
      Dear Dr B,
      Thanks for your reply and your help. I found that as it is it seems to return now only one entry and does not select randomly. I am not sure I perhaps described the problem well enough.
      I am querying 3 fields in my database, one of which is an image field. without the src, I get the result for the image field appearing as a text ref, like "images/image1.jpg". The other fields are text fields. I am trying to get all three results to appear together, with the image appearing as an image instead of a reference and then the text to appear too. I imagined that there must have to be three distinct responses in order to be able to format each individually, trouble is I dont know how to write it so that there are

      Code:
       Response.write("<tr><td>"<src="ThumbnailURL"><td></tr>")
       Response.write("<tr><td>" author "</td></tr>")
       Response.write("<tr><td>" downloadseven  "</td></tr>")
      Anyway thanks for your time and help.
      Richard

      Comment

      • omerbutt
        Contributor
        • Nov 2006
        • 638

        #4
        Originally posted by fran7
        Dear Dr B,
        Thanks for your reply and your help. I found that as it is it seems to return now only one entry and does not select randomly. I am not sure I perhaps described the problem well enough.
        I am querying 3 fields in my database, one of which is an image field. without the src, I get the result for the image field appearing as a text ref, like "images/image1.jpg". The other fields are text fields. I am trying to get all three results to appear together, with the image appearing as an image instead of a reference and then the text to appear too. I imagined that there must have to be three distinct responses in order to be able to format each individually, trouble is I dont know how to write it so that there are

        Code:
         Response.write("<tr><td>"<src="ThumbnailURL"><td></tr>")
        Response.write("<tr><td>" author "</td></tr>")
        Response.write("<tr><td>" downloadseven "</td></tr>")
        Anyway thanks for your time and help.
        Richard
        after opening the recordset you must supply the image field name to the image tag only means lets say if i have a table with three fields say
        tablename=data
        field names--------------field data
        1.images----------->images/google.gif
        2.imagename----->google image
        3.img_size--------->24 x 24
        after opening he recordset say the object is
        r now you will write
        Code:
         str="select images,imagename,img_size from data;" 
        r.open str,conn
        while not r.eof
        response.write(<tr><td align='center' valign='top'><img src='r.fields("images")' /></td></tr>)
        response.write(<tr><td align='center' valign='top'>response.write(r.fields("imagename"))</td></tr>)
        response.write(<tr><td align='center' valign='top'>response.write(r.fields("img_size"))</td></tr>)
        r.movenext
        wend
        r.close
        like this you wont get any of those errors that you were mentioning cause it will always populate the image in the image tag andall the other iinfo in the other <td> as they are appearing
        i hope that would solve the problem if i understood you correctly
        regards,
        omer
        Last edited by DrBunchman; Apr 26 '08, 05:10 PM. Reason: Added code tags

        Comment

        • fran7
          New Member
          • Jul 2006
          • 229

          #5
          Thanks for both replies, they have really helped me to understand and sort out what I needed.
          thanks
          Richard

          Comment

          • omerbutt
            Contributor
            • Nov 2006
            • 638

            #6
            Originally posted by fran7
            Thanks for both replies, they have really helped me to understand and sort out what I needed.
            thanks
            Richard
            no prob fran good to hear you solved it :) ,seee you soon
            regards,
            omer

            Comment

            Working...