Display Random Text

Ever wanted to display random tips or quotes on your website?  Well, you are in luck.  This little bit a code will display a random tip every time the browser is refreshed.

To do this, create a database called MyData and a table called tblRandomTips with these fields:
fID – autonumber
fTip – text field or memo field depending on how long your tips are

Then, start adding some helpful tips to your database.  Many websites also use a script like this to display customer testimonials, terms and definitions, trivia, etc.

Next, create a page called /RandomText.asp and copy the below code into your page:

DIM mySQL, objRS
mySQL = "SELECT MAX(fID) AS MaxTip FROM tblContent_Quick_Tips"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn

DIM intRecord
intRecord = Int(objRS("MaxTip") * Rnd + 1)
Set objRS = Nothing

mySQL2 = "SELECT fTip FROM tblContent_Quick_Tips WHERE fID = '" & intRecord & "'"
Set objRS2 = Server.CreateObject("ADODB.Recordset")
objRS2.Open mySQL2, objConn

Response.Write objRS2("fText")

Set objRS2 = Nothing

All done.  Just publish your page and database and click refresh on your browser.  Each time you refresh, a random tip will be displayed.

< Back to ASP Scripts