![]() |
| ASP Random Quote |
|
jamie
HostMySite Sales Rep
![]()
|
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
|
Jamie,
Here's a bit of code that's worked well for me... There is a
=====================
NOW... on another note, can I have the "Hostmysite Staff" or whatever under my username |
||||||||||||||||
|
|
|||||||||||||||||
| Username prefs |
|
jamie
HostMySite Sales Rep
![]()
|
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! |
||||||||||||
|
|
|||||||||||||
|
Alan
HostMySite Marketing
|
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
|
<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 |
||||||||||||
|
|
|||||||||||||
| ASP Random Quote |
|
sforker
|
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
|
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. Dom |
||||||||||||
|
|
|||||||||||||
| Checks |
|
jamie
HostMySite Sales Rep
![]()
|
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 |
|
||
|



