|
|
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
Post
#1
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.
|
![]() |
![]() ![]() Group: Members
Posts: 36 Joined: 1-March 05 From: Portugal Member No.: 4,075 |
Post
#2
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 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
Post
#3
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. |
![]() ![]() ![]() Group: Members
Posts: 46 Joined: 17-March 05 Member No.: 4,610 |
Post
#4
Mar 17 2005, 05:44 PM
One way: CODE <?
$file="base.txt"; # имя файла, где хранятся фразы $array=file($file); echo $array[rand(0,count($array)-1)]; ?> |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 2,325 Joined: 8-March 05 From: Mawson, Antarctica Member No.: 4,254 |
Post
#5
Mar 17 2005, 09:51 PM
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
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
20 | no9t9 | 6,550 | 31st May 2009 - 06:39 PM Last post by: africa |
|||
![]() |
6 | brainless | 1,267 | 27th October 2006 - 04:49 AM Last post by: quakesand |
|||
![]() |
6 | -Sky- | 267 | 8th November 2009 - 10:58 AM Last post by: kawai |
|||
![]() |
0 | kvarnerexpress | 2,588 | 28th December 2004 - 01:08 AM Last post by: kvarnerexpress |
|||
![]() |
14 | Dodger | 11,490 | 2nd November 2008 - 10:16 AM Last post by: buxgoddess |
|||
![]() |
8 | Neutrality | 5,540 | 19th August 2009 - 06:54 AM Last post by: unitechy |
|||
![]() |
3 | Amby | 3,916 | 2nd April 2005 - 04:40 PM Last post by: Milk |
|||
![]() |
9 | fsastraps | 4,840 | 31st March 2005 - 07:13 AM Last post by: Spectre |
|||
![]() |
1 | -jpsimboy- | 2,219 | 24th November 2004 - 12:20 PM Last post by: -discovertrip- |
|||
![]() |
6 | -hellomyfriends- | 2,299 | 23rd January 2005 - 04:16 AM Last post by: -hellomyfriends- |
|||
![]() |
6 | -Boomba- | 2,694 | 31st December 2004 - 10:08 AM Last post by: -l33tg33k- |
|||
![]() |
10 | -Nazrin- | 3,984 | 30th December 2004 - 08:30 AM Last post by: -l33tg33k- |
|||
![]() |
0 | KansukeKojima | 965 | 10th December 2007 - 07:31 PM Last post by: KansukeKojima |
|||
![]() |
11 | KansukeKojima | 2,111 | 30th January 2008 - 03:09 AM Last post by: hippiman |
|||
![]() |
3 | -Boomba- | 1,934 | 18th January 2005 - 04:14 AM Last post by: -Boomba- |
|||
|
Open Discussion | Time is now: 26th November 2009 - 06:25 PM |
Web Hosting Powered by ComputingHost.com. Xisto.com : Honesty ROCKS! Truth Rules.