home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

PHP random word generator help

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2254
Discussions: 200,944, Posts: 2,379,345, Members: 246,306
Old January 23rd, 2004, 11:39 AM   Digg it!   #1 (permalink)
Member
 
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

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; 
?>
And linking all 3 of them to my page with 'include'

Like this

Code:
<? include("quote1.php"); ?>
<? include("quote2.php"); ?>
-<? include("quote3.php"); ?>
Which is really a hassle, since I know that all three of these could be together..

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.
dragonz is offline   Reply With Quote
Old January 23rd, 2004, 12:08 PM     #2 (permalink)
Member
 
Carl-cox-'s Avatar
 
Join Date: Nov 2002
Location: Kent England
Posts: 395
try this

Code:
<?
$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; 

}
?>
this is the code working
http://coxeh.no-ip.com/test.php

Last edited by Carl-cox- : January 23rd, 2004 at 12:11 PM.
Carl-cox- is offline   Reply With Quote
Old January 23rd, 2004, 12:33 PM     #3 (permalink)
Member
 
Join Date: Dec 2002
Posts: 46
Quote:
Originally posted by Carl-cox-
try this

Code:
<?
$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; 

}
?>
this is the code working
http://coxeh.no-ip.com/test.php

Thank you very much, that's exactly what I was looking for.

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.
dragonz is offline   Reply With Quote
Old January 23rd, 2004, 01:04 PM     #4 (permalink)
Member
 
Carl-cox-'s Avatar
 
Join Date: Nov 2002
Location: Kent England
Posts: 395
emm put

$i = 0;

before the foreach
__________________
http://www.coxeh.com

Last edited by Carl-cox- : January 23rd, 2004 at 01:07 PM.
Carl-cox- is offline   Reply With Quote
Old January 23rd, 2004, 02:19 PM     #5 (permalink)
Member
 
shampoo's Avatar
 
Join Date: May 2002
Posts: 277
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.
shampoo is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (163)
The disrespect of Obama by Russian .. (17)
Is It Just Me? (2919)
Wireless Televisions. (11)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
Is the PSU I received dead? (11)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
How to convert MP3's (2)
windows 7 internet problem (5)
Multiple Restarts Required at Boot (0)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Laptop with wireless problem. (1)
Wireless Televisions. (11)
Have you switched yet? (86)
Asus P4G8X Mobo (5)
screen resolution vs monitor size (2)
radeon x850xt platinum & shader 3 (4)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Is the PSU I received dead? (11)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)


All times are GMT -4. The time now is 06:20 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28