Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy random text call

    hi all,
    hi, i have a problem here which i think it's relatively easy but i am not so good in asp. so i hope someone can help me.
    ok like the title of this thread, let's say i have fields of text_content and text_id(primary key). so lets say i have a few records in there like "I am trying to be useful in ASP" and "Please Help ME" and "It's Urgent". so what i want to do is to call out these records from the DB(using MS Access) and display it on the screen.....ok..that part i know....but the thing is..how can i make it randomly pick any of these records and display it and CHANGE every 5 seconds? i hope u guys get what i mean.
    pls help as it's quite urgent...have only a few days to get it done...sighzzzz....if any of u guys have the script or something, i would be more than grateful if i can have it.... .once again...thanking u guys in advance.
    Warm Regards,
    Mivec

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Can you post the codes that you've got right now?

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy the code to call random text from Db

    this is my code for calling random text from the database.it only works if u keep refreshing it. so what i want to do is that to change automatically(the text which is called from the DB) every 5 secs without refreshing. so basically it's something like an advertisement banner and the only diff is that it ad banners do with images..this one does with text from DB(Access).so how can i do that?

    Code:
    <%
    	Option Explicit
    	Response.Buffer = True
    %>
    <html>
    <head>
    	<style>
    	p { font-family:verdana; font-size:11px; }
    	</style>
    </head>
    <body>
    <br><p align="center">
    <%
    	
    	Const adCmdText = &H0001
    
    		' Connection string and SQL statement
    	Dim SQLTxt, conStr
    		SQLTxt = "select text_content from tblFrank"
    		conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
    				Server.MapPath("assignments.mdb")
    
    		' Opening database
    	Dim recset
    		Set recset = Server.CreateObject("ADODB.Recordset")
    		recset.Open SQLTxt, conStr, 3, , adCmdText
    
    		' Generating random number from total number of records
    	Dim intRnd
    		Randomize Timer
    		intRnd = (Int(RND * recset.RecordCount))
    
    		' Now moving the cursor to random record number
    		recset.Move intRnd
    
    		' Showing the random statement
    		Response.Write "<b>" & recset("text_content") & "</b>"
    
    		' Closing the database
    		recset.Close
    		Set recset = Nothing
    %>
    </p><br><br>
    <p align="center">Keep Pressing Refresh Button(F5) To See Result</p>
    </body>
    </html>
    Pls help as i am kinda lost...thanks in advance.
    Warm Regards,
    Mivec

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Try this:
    Code:
    <%
    Option Explicit
    Response.Buffer = True
    
    Function escapeToJS(ByVal str)
      If IsNull(str) Then
         escapeToJS = ""
         Exit Function
      End If
      str = Replace(str,"'","\'") 'replace ' by \'
      str = Replace(str,"""","\""") 'replace " by \"
      escapeToJS = str
    End Function
    
        Const adCmdText = &H0001
        Dim banner
    
            ' Connection string and SQL statement
        Dim SQLTxt, conStr
            SQLTxt = "select text_content from tblFrank"
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
                    Server.MapPath("assignments.mdb")
    
            ' Opening database
        Dim recset
            Set recset = Server.CreateObject("ADODB.Recordset")
            recset.Open SQLTxt, conStr, 3, , adCmdText
    
            ' Generating random number from total number of records
        Dim intRnd
            Randomize Timer
            intRnd = (Int(RND * recset.RecordCount))
    
            ' Now moving the cursor to random record number
            recset.Move intRnd
    
            ' Showing the random statement
            banner = recset("text_content") 'random initial display
    %>
    <html>
    <head>
        <style>
        p { font: bold 11px Verdana; text-align:center}
        </style>
    <script type="text/javascript">
      function rotate(){
        var i = Math.floor(Math.round()*arr.length);
        document.getElementById("banner").innerHTML = arr[i];
      }
    
      var arr = new Array();
    <%
    recset.movefirst
    while not recset.EOF
      response.write "arr[arr.length] = """ & escapeToJS(recset("text_content")) & """;" & vbCrLf
      recset.movenext
    next
    ' Closing the database
    recset.Close
    Set recset = Nothing
    %>
    </script>
    </head>
    <body onload="setInterval('rotate()', 5000)">
    <p id="banner"><%=banner%></p>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy error

    thanks glenngv for your help but there are errors in ur code.can't make head to ends whats the problem. it just pops out the error page saying

    The page cannot be displayed
    There is a problem with the page you are trying to reach and it cannot be displayed.

    --------------------------------------------------------------------------------

    Please try the following:

    Open the www21.brinkster.com home page, and then look for links to the information you want.
    Click the Refresh button, or try again later.

    Click Search to look for information on the Internet.
    You can also see a list of related sites.


    HTTP 500 - Internal server error
    Internet Explorer
    maybe you can understand better......thanks
    Warm Regards,
    Mivec

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Uncheck the "Show friendly HTTP error messages" option in the Advanced tab of Internet Options to see the detailed error.

  • #7
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy still some errors...

    hi all,
    There were some errors in your code. When I runned it, it said that there was an unexpected next in line 57 and also an expected Wend at line 58. ok that I have corrected but the code didn’t run that well. Ok..it does change after 5 secs but it goes to a word “undefined”(don’t know where this comes from) and stops there. What I need is that the random text call will always be looping forever until the user goes to the next page or something. This appears after the 1st text that is called from DB. I have attached some print screens of the output for further understanding(see zip attachment). Let’s say I want to change it’s font and colour too. I have added two more fields which are text_font and text_colour. In my previous code like this:

    Code:
    <%
    	Option Explicit
    	Response.Buffer = True
    %>
    <html>
    <head>
    	<style>
    	p { font-size:35px; color:red}
    	</style>
    </head>
    <body bgcolor="#000000">
    <br><br><br><br><br><br><br><br><p align="center">
    <%
    	
    	Const adCmdText = &H0001
    
    		' Connection string and SQL statement
    	Dim SQLTxt, conStr
    		SQLTxt = "select text_content, text_font, text_colour from tblFrank"
    		conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
    				Server.MapPath("assignments.mdb")
    
    		' Opening database
    	Dim recset
    		Set recset = Server.CreateObject("ADODB.Recordset")
    		recset.Open SQLTxt, conStr, 3, , adCmdText
    
    		' Generating random number from total number of records
    	Dim intRnd
    		Randomize Timer
    		intRnd = (Int(RND * recset.RecordCount))
    
    		' Now moving the cursor to random record number
    		recset.Move intRnd
    
    		' Showing the random statement
    		'While Not recset.EOF
    		Response.Write "<b><font face="&recset("text_font")&"><font color="&recset("text_colour")&">" & recset("text_content") & "<br> Using Font : "&recset("text_font") &"</font></font></b>"
            'Wend
    		' Closing the database
    		recset.Close
    		Set recset = Nothing
    %>
    
    </p><br>
    <p align="center">Keep Pressing Refresh Button(F5) To See Result</p>
    </body>
    </html>
    this will make the colour and font change every time u refresh the page. But I want this to follow the changing of text too in the 5 second change. How can I put it into the code you gave me? This is the code u gave me(corrected the ‘next’ error):

    Code:
    <%
    Option Explicit
    Response.Buffer = True
    
    Function escapeToJS(ByVal str)
      If IsNull(str) Then
         escapeToJS = ""
         Exit Function
      End If
      str = Replace(str,"'","\'") 'replace ' by \'
      str = Replace(str,"""","\""") 'replace " by \"
      escapeToJS = str
    End Function
    
        Const adCmdText = &H0001
        Dim banner
    
            ' Connection string and SQL statement
        Dim SQLTxt, conStr
            SQLTxt = "select text_content, text_font, text_colour from tblFrank"
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
                    Server.MapPath("assignments.mdb")
    
            ' Opening database
        Dim recset
            Set recset = Server.CreateObject("ADODB.Recordset")
            recset.Open SQLTxt, conStr, 3, , adCmdText
    
            ' Generating random number from total number of records
        Dim intRnd
            Randomize Timer
            intRnd = (Int(RND * recset.RecordCount))
    
            ' Now moving the cursor to random record number
            recset.Move intRnd
    
            ' Showing the random statement
            banner = recset("text_content") 'random initial display
    %>
    <html>
    <head>
        <style>
        p { font: bold 11px Verdana; text-align:center}
        </style>
    <script type="text/javascript">
      function rotate(){
        var i = Math.floor(Math.round()*arr.length);
        document.getElementById("banner").innerHTML = arr[i];
      }
    
      var arr = new Array();
    <%
    recset.movefirst
    while not recset.EOF
      response.write "arr[arr.length] = """ & escapeToJS(recset("text_content")) & """;" & vbCrLf
      recset.movenext
    Wend
    ' Closing the database
    recset.Close
    Set recset = Nothing
    %>
    </script>
    </head>
    <body bgcolor="#FFFFFF" onload="setTimeout('rotate()', 5000)">
    <p id="banner"><font face="recset("text_font")"><font color="recset("text_colour")"><%=banner%></font></font></p>
    </body>
    </html>
    the code which u just seen doesn’t change the font or the colour where it stays at one font and only one colour. So how can I do it where the font and colour changes accordingly to the change of text in every 5 secs? Any one pls pls pls help…..it’s already the due date….and I can’t figure out how………
    Warm Regards,
    Mivec

  • #8
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry...i forgot to post the attachment...here it is...
    Attached Files Attached Files
    Warm Regards,
    Mivec

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    <%
    Option Explicit
    Response.Buffer = True
    
    Function escapeToJS(ByVal str)
      If IsNull(str) Then
         escapeToJS = ""
         Exit Function
      End If
      str = Replace(str,"'","\'") 'replace ' by \'
      str = Replace(str,"""","\""") 'replace " by \"
      escapeToJS = str
    End Function
    
        Const adCmdText = &H0001
        Dim banner, bannerFont, bannerColor, ctr
    
            ' Connection string and SQL statement
        Dim SQLTxt, conStr
            SQLTxt = "select text_content, text_font, text_colour from tblFrank"
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
                    Server.MapPath("assignments.mdb")
    
            ' Opening database
        Dim recset
            Set recset = Server.CreateObject("ADODB.Recordset")
            recset.Open SQLTxt, conStr, 3, , adCmdText
    
            ' Generating random number from total number of records
        Dim intRnd
            Randomize Timer
            intRnd = (Int(RND * recset.RecordCount))
    
            ' Now moving the cursor to random record number
            recset.Move intRnd
    
            ' Showing the random statement
            banner = recset("text_content") 'random initial display
    	bannerFont = recset("text_font")
    	bannerColor = recset("text_colour")
    %>
    <html>
    <head>
    <script type="text/javascript">
      function rotate(){
         var bnr = document.getElementById("banner");
         var i = Math.floor(Math.random()*arr.length);
         bnr.innerHTML = arr[i].content;
         bnr.style.color = arr[i].color;
         bnr.style.fontFamily = arr[i].font;     
      }
    
      var arr = new Array();
    <%
    ctr=0
    recset.movefirst
    while not recset.EOF
      response.write "arr[" & ctr & "] = new Object();" & vbCrLf
      response.write "arr[" & ctr & "].content = """ & escapeToJS(recset("text_content")) & """;" & vbCrLf
      response.write "arr[" & ctr & "].font = """ & recset("text_font") & """;" & vbCrLf
      response.write "arr[" & ctr & "].color = """ & recset("text_colour") & """;" & vbCrLf
      ctr = ctr + 1
      recset.movenext
    Wend
    ' Closing the database
    recset.Close
    Set recset = Nothing
    %>
    </script>
    <style>
       p { 
             font: bold 11px <%=bannerFont%>; 
             color: <%=bannerColor%>;
             text-align:center;
       }
    </style>
    </head>
    <body bgcolor="#FFFFFF" onload="setTimeout('rotate()', 5000)">
     <p id="banner"><%=banner%></p>
    </body>
    </html>


  •  

    Posting Permissions

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