SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pick five records at random from db and rotate them weekly

    Hi,

    I need to pull five records randomly from a database, and display them on a web page. These need to change weekly.

    Is there a way to do this in ASP?

    Any help would be great, ta...

  2. #2
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    South Russia/UK
    Posts
    723
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These need to change weekly
    this line confuses me. If they are random , they will be changing on every call

    N_F_S

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really quite simple. Each week it needs to pull out five random results from the database, then display these same results for the whole week.

    I know how to pull out random records each time the page is hit, but this isn't what I'm looking for. I'm looking to change the records once per week, with a new set of randomly picked records.

    Does that make sense?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your server will be up 24/7 (or it's ok that your values reset on server reset) and you don't mind using application variables:

    Code:
    <%
    Function GetRandomRecords()
    	If Len(Application("lastupdate")) = 0 Or DateAdd("w","1", Application("lastupdate")) < Now() Then
    		Set oDict = Server.CreateObject("Scripting.Dictionary")
    
    		Set oDB = Server.CreateObject("ADODB.Connection")
    		oDB.open MyConnectString
    		
    		' *** Replace this part with code to pull 5 random records from a DB, and add them to dictionary object
    		
    		Set oRS = oDB.Execute("SELECT TOP 5 MyRecord FROM MyTable")
    		
    		nCount = 1
    		
    		Do While Not oRS.EOF
    			oDict.Add CStr(nCount), oRS.Fields("MyRecord").Value
    			oRS.MoveNext
    			nCount = nCount + 1
    		Loop
    		
    		' ***
    		
    		Application("lastupdate") = Now()
    		Set Application("randomrecords") = oDict
    		
    	End If
    
    	Set GetRandomRecords = Application("randomrecords")
    
    End Function
    
    Set oRecs = GetRandomRecords()
    
    For Each vKey In oRecs.Keys
    	Response.Write oRecs.Item(vKey) & "<br/>"
    Next
    %>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •