Thread: PHP random word generator help
-
January 23rd, 2004, 11:39 AM #1Member
- Join Date
- Dec 2002
- Posts
- 46
PHP random word generator help
Will anyone help me make this into a random word generator that makes three random words like this:
RandomWord1 RandomWord2 -(dash) RandomWord3
Right now I'm using three PHP files like this
And linking all 3 of them to my page with 'include'Code:<? $delim = "\n"; $quotefile = "quotes1.txt"; $fp = fopen($quotefile, "r"); $contents = fread($fp, filesize($quotefile)); $quote_arr = explode($delim,$contents); fclose($fp); srand((double)microtime()*1000000); $quote_index = (rand(1, sizeof($quote_arr)) - 1); $herequote = $quote_arr[$quote_index]; echo $herequote; ?>
Like this
Which is really a hassle, since I know that all three of these could be together..Code:<? include("quote1.php"); ?> <? include("quote2.php"); ?> -<? include("quote3.php"); ?>
I was wondering if anyone could help me kinda merge all 3 PHP files into one simple script so I would only have to 'include' one PHP script that displays all three random words (the third seperated from the rest by a dash).
I'm a total PHP newbie so please try not to laugh.
Thanks
EDIT: Oh, and basically the quote1.txt, quote2.txt, and quote3.txt files that I am using to get my random quotes from use a single quote on every line and pick a random line for each quote.. I would like to have one PHP script that takes the random words from 3 seperate text files and picks a random line to display from the .txt file.
Bet I confused you even more.. Sorry.Last edited by dragonz; January 23rd, 2004 at 11:43 AM.
-
January 23rd, 2004, 12:08 PM #2
try this
this is the code workingCode:<? $files = array('quotes1.txt', 'quotes2.txt', 'quotes3.txt'); $delim = "\n"; foreach($files as $quotefile){ $i++; if($i == 3){ print " - "; }else{ print " "; } $fp = fopen($quotefile, "r"); $contents = fread($fp, filesize($quotefile)); $quote_arr = explode($delim,$contents); fclose($fp); srand((double)microtime()*1000000); $quote_index = (rand(1, sizeof($quote_arr)) - 1); $herequote = $quote_arr[$quote_index]; echo $herequote; } ?>
http://coxeh.no-ip.com/test.phpLast edited by Carl-cox-; January 23rd, 2004 at 12:11 PM.
-
January 23rd, 2004, 12:33 PM #3Member
- Join Date
- Dec 2002
- Posts
- 46
Thank you very much, that's exactly what I was looking for.Originally posted by Carl-cox-
try this
this is the code workingCode:<? $files = array('quotes1.txt', 'quotes2.txt', 'quotes3.txt'); $delim = "\n"; foreach($files as $quotefile){ $i++; if($i == 3){ print " - "; }else{ print " "; } $fp = fopen($quotefile, "r"); $contents = fread($fp, filesize($quotefile)); $quote_arr = explode($delim,$contents); fclose($fp); srand((double)microtime()*1000000); $quote_index = (rand(1, sizeof($quote_arr)) - 1); $herequote = $quote_arr[$quote_index]; echo $herequote; } ?>
http://coxeh.no-ip.com/test.php
But can you help me figure out why I get this error message?
"Notice: Undefined variable: i in (thephpfile) on line 6"
It comes out to be this bit
$i++;
And thanks again for helping me with the script
EDIT: The script does work very well, but I wonder why I get that error message..Last edited by dragonz; January 23rd, 2004 at 12:36 PM.
-
January 23rd, 2004, 01:04 PM #4
emm put
$i = 0;
before the foreachLast edited by Carl-cox-; January 23rd, 2004 at 01:07 PM.
-
January 23rd, 2004, 02:19 PM #5
It works great! This is exactly what I wanted.. Thank you very much.
I've been looking for something like this for a long time.Last edited by shampoo; January 23rd, 2004 at 02:23 PM.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)



LinkBack URL
About LinkBacks



Reply With Quote

Implying a knife, ball bat, cast-iron pan, taser, or running the hell away would not have also been effective. She had the means, just not the intelligence to utilize her surroundings to her...
Do YOU think having a gun would...