+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    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.

  2. #2
    Member Carl-cox-'s Avatar
    Join Date
    Nov 2002
    Location
    Kent England
    Posts
    396
    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.

  3. #3
    Member
    Join Date
    Dec 2002
    Posts
    46
    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.

  4. #4
    Member Carl-cox-'s Avatar
    Join Date
    Nov 2002
    Location
    Kent England
    Posts
    396
    emm put

    $i = 0;

    before the foreach
    Last edited by Carl-cox-; January 23rd, 2004 at 01:07 PM.

  5. #5
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews