1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php select and display random word from list

Discussion in 'PHP' started by tony84, Feb 12, 2006.

  1. #1
    Im pretty new to php, what im wanting is a little script that will scan through a text file and display 1 of the words randomly. Every time the page is refreshed the link will change, does anyone know where i can get something like this from?

    Tony
     
    tony84, Feb 12, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you had the list of words in an sql database then you could use a select statement to "select * from tablename orderby rand limit 1";
     
    mad4, Feb 12, 2006 IP
  3. tony84

    tony84 Well-Known Member

    Messages:
    1,864
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    140
    #3
    i would be happy to do it with mysql but im useless with phpbb and mysql so im trying to make it as easy as possibly for myself and so i would need a form for people to complete to add the details to the new forum which seems a lot harder to code
     
    tony84, Feb 12, 2006 IP
  4. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #4
    Try something like:

    $filename = "words.txt";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    $words = explode("\r", $contents);
    echo $words[rand(0, count($words)-1)];
    PHP:
     
    Lordo, Feb 14, 2006 IP
  5. seopup

    seopup Peon

    Messages:
    274
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    $filename="words.txt";
    $words=file($filename);
    shuffle($words);
    $word=$words[0];
    echo $word;
    
    PHP:
     
    seopup, Feb 15, 2006 IP