• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

ASP random text script

I am in need of an ASP script that will select a different line of text from a plain text file each time a web page is loaded and insert that text into the web page.
0
ficken
Asked:
ficken
  • 4
  • 3
  • 2
1 Solution
 
xavCommented:
Try this:

<% @LANGUAGE="JScript" %>
<html><head>
<title></title>
<%
//If you know the path, use:
//cFileName="c:\\inetpub\\wwwroot\\testing\\quotes.txt"
//or, get the path from the current one
thisScriptName=new String(Request.ServerVariables("PATH_TRANSLATED"));
aDirs=thisScriptName.split("\\");
//Chop off the file name
scriptPath="";
for (i=0;i<aDirs.length-1;i++) {
      scriptPath+=aDirs[i]+"\\";
}
cFileName=scriptPath+"quotes.txt";

//this will read a file (quotes.txt) with one quote per line:
//For example,...
//Hello
//How are you
//What's new
fs = new ActiveXObject("Scripting.FileSystemObject");
readFile = fs.OpenTextFile(cFileName);
iTotalLines=0;
aQuotes=new Array();
//Create an array of quotes
while (!readFile.AtEndOfStream)      {
      curLine = readFile.ReadLine( );
      aQuotes[iTotalLines]=curLine
      iTotalLines++;
}
readFile.Close();
rndNum=Math.floor(Math.random()*iTotalLines)
Response.Write(aQuotes[rndNum]);
%>
</body>
</html>
0
 
MasseyMCommented:
Is this for random quotes?  If so, I have a database driven quote machine...

If you would like, I will email it to you...
0
 
fickenAuthor Commented:
Your quote machine sounds interesting, how easy and what is involved in implementation?

The first comment listed sounds a little bit questionable to me, I have reservations about including ActiveX controls on our site. I would prefer to stick with ASP/Javascript.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
xavCommented:
The line:

fs = new ActiveXObject("Scripting.FileSystemObject");

Just calls the MS FileSystem object on the server.

You can replace it with:

fs = Server.CreateObject("Scripting.FileSystemObject");

It's just a reference to a COM object--the same as if you open a database:

conn=Server.CreateOject("ADODB.Recordset")
0
 
MasseyMCommented:
Very easy really.  YOu have a database with quotes.  The ASp randomly selects one and it is then displayed in a table...

quotedsn.asp
-------------
<%
  ' This is the entire DB path - Physical with respect to root physical path
  sQuoteDB   = "Quotes.mdb"

  sQuoteScriptDir = "/Quotes/" ' Change for your directory...

  ' Set the virtual Directory
  sQuotePath = Server.MapPath(sQuoteScriptDir) & "\"

  ' This is the resulting DSN string
  sQuoteDSN = "filedsn=" & sQuotePath & "Quotes.Dsn;DefaultDir=" & sQuotePath & ";DBQ=" & sQuotePath & sQuoteDB & ";"
%>

quotes.dsn
-------------
[ODBC]
DRIVER=Microsoft Access Driver (*.mdb)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=8
MaxBufferSize=512
ImplicitCommitSync=Yes
FIL=MS Access
DriverId=25

quote.asp
-------------
<% Server.ScriptTimeOut = 15 %>
<!-- #include File="QuoteDSN.Asp" -->
<%
      Set QConn = Server.CreateObject("ADODB.Connection")
      QConn.Open sQuoteDSN
      quotesql="SELECT * FROM Quotes"
      Set QuoteRS = Server.CreateObject("ADODB.Recordset")
      QuoteRS.Open quotesql, QConn, 3, 3
      NumQuotes = QuoteRS.RecordCount
      QuoteRS.close
%>
<%
Randomize
RNum = Int((NumQuotes * Rnd) + 1)
%>
<%
      sql2="SELECT * FROM Quotes WHERE ID=" & RNum
      QuoteRS.Open sql2, QConn, 3, 3
%>
<% =QuoteRS("Quote") %>
<%
QuoteRS.close
QConn.close
set QuoteRS = nothing
set QConn = nothing
sDSN = ""
%>

quotes.mdb
------------
1 table:

Quotes [Table]
  ID     - Autonumber
  Quote  - memo
  Author - Text(255)
 
0
 
fickenAuthor Commented:
On second thought, rather than having two completely different scripts on the site to do similar things, I thought I would take a script that we have for placing a random image onto a page and modify it to place random snippets of text onto a page, however it does not seem to be working, perhaps someone can troubleshoot this for me...

Here is the original random image script that does work:

<!--Begin ASP Random Image script-->

<%
ThisFile = Request.ServerVariables("PATH_TRANSLATED")
LastSlash = InstrRev(ThisFile, "\")
ThisDir = Left(ThisFile,LastSlash)

strFileName = ThisDir + "images.asp"
Set FileObject = CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile(strFileName)


Dim Images(20)

i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  i = i + 1
Loop

MaxImages = i - 1
Randomize Second(time())
intRandomNumber = Int((MaxImages) * Rnd + 1)  

'cdr 9/2/99 close & release these objects!!!!!!

set FileObject = Nothing
set ReadStream = Nothing

%>


<!--End ASP Random Image script-->




and here is my modification which does not work, all i get is a page with nothing inbetween  the two comment tags:


<!--Begin ASP Random Text script-->

<%
ThisFile = Request.ServerVariables("PATH_TRANSLATED")
LastSlash = InstrRev(ThisFile, "\")
ThisDir = Left(ThisFile,LastSlash)

strFileName = ThisDir + "quotes.asp"
Set FileObject = CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile(strFileName)


Dim Text(16)

i = 1
Do While Not ReadStream.AtEndOfStream
  Text(i) = ReadStream.ReadLine
  i = i + 1
Loop

MaxText = i - 1
Randomize Second(time())
intRandomNumber = Int((MaxText) * Rnd + 1)  

'cdr 9/2/99 close & release these objects!!!!!!

set FileObject = Nothing
set ReadStream = Nothing

%>


<!--End ASP Random Text script-->
0
 
xavCommented:
I think all you need to do is write it out:

response.write(Text(intRandomNumber))

before the ASP end tag.
0
 
fickenAuthor Commented:
That did it, thanks for your help, sorry if i wasted your time.
0
 
xavCommented:
Not at all.  Glad I could help.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now