|
|
|
|
![]() ![]() |
Mar 14 2005, 08:44 PM
Post
#1
|
|
|
Moderator ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
<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.
|
|
|
|
Mar 14 2005, 09:27 PM
Post
#2
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 36 Joined: 1-March 05 From: Portugal Member No.: 4,075 |
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 |
|
|
|
Mar 14 2005, 10:54 PM
Post
#3
|
|
|
Moderator ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
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. |
|
|
|
Mar 17 2005, 05:44 PM
Post
#4
|
|
|
Newbie [Level 3] ![]() ![]() ![]() Group: Members Posts: 46 Joined: 17-March 05 Member No.: 4,610 |
One way:
CODE <?
$file="base.txt"; # имя файла, где хранятся фразы $array=file($file); echo $array[rand(0,count($array)-1)]; ?> |
|
|
|
Mar 17 2005, 09:51 PM
Post
#5
|
|
|
Moderator ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
You, my friend, are THE man!
I case can't tell by my reaction, OwrLam's code works! Thank you so much, and welcome to trap17. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th September 2008 - 11:55 PM |