Reply to topic
ASP Random Quote
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 766
Location: Newark, De
Reply with quote
I'm thinking about actually working on my website for a change, and one of the things that I wanted to incorporate is a random quote at the top of the page. Can anyone give me any tips on how to do this in ASP?

I know that I would need to pull the quotes from a database, but how would I randomize it? Also, is there a way to format the quotes with line breaks in the database, so I could display different types properly? Like this:

"I took the one less traveled, and it has made all the difference." - Robert Frost

"Please allow me to introduce myself
I'm a man of wealth and taste
I've been around a long long time
Ruined many a man's humble faith"

- The Doors, "Sympathy for the Devil"
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
Jamie,

Here's a bit of code that's worked well for me... There is a
Code:
Randomize()
funtion native to ASP, you just have to know how to use it. Basically you'll read all of your records into an array, use 0 and UBOUND(Your_Array) as the min and max, get a random index pulled and then display the quote... PIECE O CAKE!!!

=====================
Code:
<%
Dim intLowerBound    ' Lower bound of the random number range
Dim intUpperBound    ' Upper bound of the random number range

Dim intRangeSize     ' Size of the range
Dim sngRandomValue   ' A random value from 0 to intRangeSize
Dim intRandomInteger ' Our final result - random integer to return


' Retrieve lower and upper bound requests if they're there
' o/w set to defaults of 0 and 100
If IsNumeric(Request.QueryString("lowerbound")) Then
   intLowerBound = CLng(Request.QueryString("lowerbound"))
Else
   intLowerBound = 0
End If

If IsNumeric(Request.QueryString("upperbound")) Then
   intUpperBound = CLng(Request.QueryString("upperbound"))
   
   ' Add a line to deal with default case of 0 to 0.
   ' This really isn't neccessary, but I do it so the
   ' sample doesn't default to generating a number between
   ' 0 and 0 and always return 0 when no bounds are provided.
   If intLowerBound = 0 And intUpperBound = 0 Then intUpperBound = 100
Else
   intUpperBound = 100
End If


' Check for people asking for a number from in an inappropriate
' range (ie: 50 to 10) and swap the bounds
If intLowerBound > intUpperBound Then
   ' I really should've declared a temporary variable for this
   ' swapping, but I was lazy and this one was already defined
   ' and I don't use it till later... oh all right I'll do it
   ' the "right" way... actually even this is bad... I should've
   ' defined this up top... so sue me... hey it's free code what
   ' do you want from me?
   Dim iTemp
   iTemp = intLowerBound
   intLowerBound = intUpperBound
   intUpperBound = iTemp
End If


' Initialize the random number generator.
' Randomize can actually take parameters telling it how to initialize
' things, but for the most you'll just want to call it without passing
' it anything.
Randomize()

' Generate our random number.
' The Rnd function does most of the work.  It returns a value in the
' range 0 <= value < 1 so to generate a random integer in the specified
' range we need to do some calculation.  Specifically we take the size
' of the range in which we want to generate the number (add 1 so the
' upper bound can be generated!) and then multiply it by our random
' element.  Then to place the value into the correct range of numbers
' we add the lower bound.  Finally we truncate the number leaving us
' with the integer portion which is always somewhere between the
' lower bound and upper bound (inclusively).

' Find range size
intRangeSize = intUpperBound - intLowerBound + 1

' Get a random number from 0 to the size of the range
sngRandomValue = intRangeSize * Rnd()

' Center the range of possible random numbers over the desired result set
sngRandomValue = sngRandomValue + intLowerBound

' Convert our value to an integer
intRandomInteger = Int(sngRandomValue)


' The above 4 lines are equivilent to the popular shorter version
' below.  I split it up so I could indicate what each step is doing.
' intRandomInteger = Int((intUpperBound - intLowerBound + 1) * Rnd + intLowerBound)

' Show out output indicating what we've done and our result.
%>
You asked for a random number between <B><%= intLowerBound %></B> and <B><%= intUpperBound %></B>.<BR>
The computer returned: <B><%= intRandomInteger %></B><BR>

<!-- Build the form for user input -->
<FORM ACTION="random_number.asp" METHOD="get" NAME="frmRandomNumberBounds">

Generate a random number between
<INPUT TYPE="text" NAME="lowerbound" VALUE="<%= intLowerBound %>" SIZE="5" MAXLENGTH="5"></INPUT>
 and
<INPUT TYPE="text" NAME="upperbound" VALUE="<%= intUpperBound %>" SIZE="5" MAXLENGTH="5"></INPUT>

<INPUT TYPE="submit"></INPUT>

</FORM>




NOW... on another note, can I have the "Hostmysite Staff" or whatever under my username Wink
Username prefs
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 766
Location: Newark, De
Reply with quote
LOL....that's great. We build this forum for clients, and I decide to post a question that gets answered by another tech. There's something wrong here, I just have to find it...

...thanks, regardless. Now I just have to figure out your code. Did I mention that I don't really program my own code, only troubleshoot others'? <shrugs> What can I say, I'm good at what I do! Laughing
Alan
HostMySite Marketing

Joined: 08 Mar 2004
Posts: 126
Reply with quote
There is a much easier way to do it.

In your database create a table that stores the quotes and give the table an ID number set to autonumber. I usually make this field the primary key as well.

In your code you can do the following:

<% set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSNname"

'calculate total number of quotes as well and randomizer seed
set quote = conn.execute("Select * from table")
randomseed = 1
do until quote.eof
randomseed = randomseed + 1
quote.MoveNext
loop

' the variable randomseed will not contain the total number of quotes in your table

'select a random quote number between 1 and randomseed
randomquote =int(rnd*randomseed)+1
set quote = conn.execute("Select quote from table where ID = randomquote")

'display your quote
Response.Write("Here is my quote:<br>" & quote("quote"))
%>

The above snipet of code should first select all of the quotes from your table. Then it will increment randomseed by 1 for each quote until it reaches the end of the file. This will give you the total number of quotes. You can do this a different why by simply moving to the last record and reading the ID number, but if you delete a quote this number wont be accurate. Once it has the randomseed it will pick a random number between 1 and the seed. Next the code will query the database for that specific quote and finally display the quote.

This is probably similar to what Josh posted, just simpler.

If it doesnt work let me know and I will go over it with you in the office and then post any necessary corrections.

As for displaying the quotes in a specific format it all depends on how you want to them displayed. You can use Response.Write with HTML tags or you can embed the ASP output within normal HTML tags.
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
<shady> OOOHHHH!!!! *steals code for his own library* </shady>

I like that answer!!! And you're right... that's ALOT simpler... I'd go with that solution, personally Very Happy btw, mine's so long partially because it's well commented/documented - which means it's perfect for the copy & paste coder Wink
ASP Random Quote
sforker


Joined: 27 Apr 2004
Posts: 4
Reply with quote
As another solution, you can do this in a Stored Procedure in SQL Server. I'm using SQL Server 2000 Specific features in this solution.

It's pretty self describing, I make a temp table only because you can't assume that the indentity column is always in order, it might have gaps in it, so this allows me to make a new identity column that is 1 to max records.

the Table variable is new in SQL Server 2000 and is less costly then a standard Temp Table.

This is the stored procedure code to retrieve a Tip-of-the-day.....

Create proc SP_GET_RANDOM_TOTD
As
Begin
If Exists(Select APP_TOTD_ID from APP_TOTD) /* Do we have any records ?? */
Begin
/* temp table in order to randomize the Tip(s)-of-the-day */
Declare @TempTable table (colID int identity(1,1) Not Null,
colTOTD_ID int Not Null)

Insert Into @TempTable (colTOTD_ID)
select APP_TOTD_ID From APP_TOTD

Declare @MAX_ITEMS int /* Top Number for Random Selection */
Declare @RND int /* Random Number selection */

Select @MAX_ITEMS = Max(colID) From @TempTable
Set @RND = (SELECT CAST((rand() * @MAX_ITEMS) as int) + 1)

/* Select and return the Tip-of-the-day */
Select APP_TOTD_ID as 'AppTotdId',
APP_TOTD_Title as 'AppTotdTitle',
APP_TOTD_Text as 'AppTotdText'
From APP_TOTD
Where APP_TOTD_ID = @RND
End /* End, the Totd Table is Empty - Guess we are out of tips */
End /* End of Procedure */
go
pccig


Joined: 07 Apr 2004
Posts: 4
Reply with quote
another solution is to look at http://www.hairebohmer.com/home.asp (you host it, so just look at the ASP file and grad the code. I have SQL database of quotes (you have access to that too).

good luck. If you have any problems just call Jamie at 877-215-4678 he can help you debug it.
Laughing
Dom
Checks
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 766
Location: Newark, De
Reply with quote
Lol. I guess I should be used to others writing checks on me - I once had a client demand that I restore their website when the entire East Coast was experiencing an outage.

Wait right there while I fix the Internet. You know, the one that Bob Doyle created? Yeah, that one.

Seriously though, thanks for the code tip - never thought that the exact app I wanted to create is already on our servers!
ASP Random Quote
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic