»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 01-23-2004, 08:39 AM   #1 (permalink)
Registered User
 
Join Date: Dec 2002
Posts: 36
dragonz is on a distinguished road
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; 01-23-2004 at 08:43 AM.
dragonz is offline   Reply With Quote
Old 01-23-2004, 09:08 AM   #2 (permalink)
Registered User
 
Carl-cox-'s Avatar
 
Join Date: Nov 2002
Location: Kent England
Posts: 235
Carl-cox- is on a distinguished road
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-; 01-23-2004 at 09:11 AM.
Carl-cox- is offline   Reply With Quote
Old 01-23-2004, 09:33 AM   #3 (permalink)
Registered User
 
Join Date: Dec 2002
Posts: 36
dragonz is on a distinguished road
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; 01-23-2004 at 09:36 AM.
dragonz is offline   Reply With Quote
Old 01-23-2004, 10:04 AM   #4 (permalink)
Registered User
 
Carl-cox-'s Avatar
 
Join Date: Nov 2002
Location: Kent England
Posts: 235
Carl-cox- is on a distinguished road
emm put

$i = 0;

before the foreach

Last edited by Carl-cox-; 01-23-2004 at 10:07 AM.
Carl-cox- is offline   Reply With Quote
Old 01-23-2004, 11:19 AM   #5 (permalink)
Registered User
 
shampoo's Avatar
 
Join Date: May 2002
Posts: 227
shampoo is on a distinguished road
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; 01-23-2004 at 11:23 AM.
shampoo is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 07:51 AM.