IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet

Generate Random Phrases

, ...from a txt file


snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #1 post Mar 14 2005, 08:44 PM
<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://www.trap17.com/forums/Random-Messag...ator-t7603.html

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

#!/usr/bin/perl -wT
use 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
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.
Go to the top of the page
+Quote Post
 
Start new topic
Replies (1 - 4)
Blix
no avatar
Newbie [Level 2]
**
Group: Members
Posts: 36
Joined: 1-March 05
From: Portugal
Member No.: 4,075



Post #2 post Mar 14 2005, 09:27 PM
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[i]);
//-->
</SCRIPT>
have fun
Blix
Go to the top of the page
+Quote Post
snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #3 post Mar 14 2005, 10:54 PM
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.
Go to the top of the page
+Quote Post
OwrLam
no avatar
Newbie [Level 3]
***
Group: Members
Posts: 46
Joined: 17-March 05
Member No.: 4,610



Post #4 post Mar 17 2005, 05:44 PM
One way:


CODE
<?
$file="base.txt"; # имя файла, где хранятся фразы

$array=file($file);
echo $array[rand(0,count($array)-1)];
?>
Go to the top of the page
+Quote Post
snlildude87
no avatar
Moderator
***************
Group: Members
Posts: 2,325
Joined: 8-March 05
From: Mawson, Antarctica
Member No.: 4,254



Post #5 post Mar 17 2005, 09:51 PM
You, my friend, are THE man! laugh.gif

I case can't tell by my reaction, OwrLam's code works!

Thank you so much, and welcome to trap17. smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   20 no9t9 6,550 31st May 2009 - 06:39 PM
Last post by: africa
No New Posts   6 brainless 1,267 27th October 2006 - 04:49 AM
Last post by: quakesand
No New Posts   6 -Sky- 267 8th November 2009 - 10:58 AM
Last post by: kawai
No New Posts   0 kvarnerexpress 2,588 28th December 2004 - 01:08 AM
Last post by: kvarnerexpress
No new   14 Dodger 11,490 2nd November 2008 - 10:16 AM
Last post by: buxgoddess
No New Posts   8 Neutrality 5,540 19th August 2009 - 06:54 AM
Last post by: unitechy
No New Posts   3 Amby 3,916 2nd April 2005 - 04:40 PM
Last post by: Milk
No New Posts   9 fsastraps 4,840 31st March 2005 - 07:13 AM
Last post by: Spectre
No New Posts   1 -jpsimboy- 2,219 24th November 2004 - 12:20 PM
Last post by: -discovertrip-
No New Posts   6 -hellomyfriends- 2,299 23rd January 2005 - 04:16 AM
Last post by: -hellomyfriends-
No New Posts   6 -Boomba- 2,694 31st December 2004 - 10:08 AM
Last post by: -l33tg33k-
No New Posts   10 -Nazrin- 3,984 30th December 2004 - 08:30 AM
Last post by: -l33tg33k-
No New Posts   0 KansukeKojima 965 10th December 2007 - 07:31 PM
Last post by: KansukeKojima
No New Posts   11 KansukeKojima 2,111 30th January 2008 - 03:09 AM
Last post by: hippiman
No New Posts   3 -Boomba- 1,934 18th January 2005 - 04:14 AM
Last post by: -Boomba-


 



RSS Open Discussion Time is now: 26th November 2009 - 06:25 PM

Web Hosting Powered by ComputingHost.com. Xisto.com : Honesty ROCKS! Truth Rules.