Jump to content
xisto Community
Sign in to follow this  
snlildude87

Generate Random Phrases ...from a txt file

Recommended Posts

<question>
I have a question on how to generate random phrases (facts, quotes, etc) which the server will randomly pick from txt file. Normally, I would use SSI to accomplish this task, but I recently added a blog (Nucleus) to my site which uses PHP (.php file). So my question is, how can you display random phrases in which the server will pick from a specified text file?

Thanks!
</end question>

I saw a thread very similar to my problem, but the code supplied by Inspired does not allow the server to select the phrase from a file. The only reason I want this is because I have over 100 quotes, and putting all 100 in a big php file would be a hassel.
Thread: http://forums.xisto.com/topic/7603-random-message-generator/

In case you're interested, this is how you can generate random facts using SSI:

#!/usr/bin/perl -wTuse strict;my $quote;open(QUOTES, "facts.txt") or die "Oops! Can't find quote file: $!";srand;rand($.) < 1 && ($quote = $_) while <QUOTES>;print "Content-type: text/html\n\n";print $quote;
Then you have a name the code above to facts.cgi and create a facts.txt file (or any name you wish) where you will store all your phrases. You must have one phrase per line.

After that, place the code
<!--#include virtual="cgi-bin/facts.cgi"-->
in a place in your web page that you want a chosen phrase to appear, change the permission of facts.cgi to 755, rename your web page to xxx.cgi, and upload the three files.

Share this post


Link to post
Share on other sites

Hi, you can do this very easy with javascript...

 

just an example:

Put the following text into the body tag of your website

<script LANGUAGE="JavaScript">

 

<!--

QuoteText = new Array(3);

QuoteText[0] = 'text1';

QuoteText[1] = 'text2';

QuoteText[2] = 'text3';

 

i = Math.floor(Math.random() * QuoteText.length);

document.write(QuoteText);

//-->

</SCRIPT>

have fun

Blix

Share this post


Link to post
Share on other sites

Blix, thanks for the reply, but the phrase would not show if a person has JavaScript disabled because JavaScript can sometimes be intrusive.PHP, however, will always show because the server processes the request, not the browser.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.