Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default Random echo: PHP

    Right. I'm useless with PHP so I'll explain what I would like..

    Say I've got loads of different lines of song lyrics. I want to put these lyrics into a php script which would chose a random one each time the page is loaded / refreshed and echoed to the page.

    Should be simple, I used to know how to do this but my old age has got the better of me (lol, jk).

    Thanks, +REP to those who assist.
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


  2. #2
    Join Date
    Nov 2006
    Location
    D?sseldorf
    Posts
    2,858
    Tokens
    2,256

    Latest Awards:

    Default

    Put all the song lyrics on lines in notepad, save as TXT file

    PHP Code:
    <?php
    $quotes 
    "lyrics.txt";
    $quotes file($quotes);
    srand((double)microtime() * 1000000);
    $ranNum rand(0count($quotes)-1);
    echo (
    $quotes[$ranNum]);
    ?>

  3. #3
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default

    Thank you. Perfect

    REP / thread closed.
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


  4. #4
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Simpler way

    PHP Code:
    <?php
    $array 
    = array("LINE1""LINE2""LINE3","etc..");

    echo 
    $arrayrand0count$array ) - ) ];
    ?>
    should work, untested

  5. #5
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Quote Originally Posted by Blob View Post
    Simpler way

    PHP Code:
    <?php
    $array 
    = array("LINE1""LINE2""LINE3","etc..");

    echo 
    $arrayrand0count$array ) - ) ];
    ?>
    should work, untested
    Then he has to put all those lyrics into that array... which is time consuming; as well as pointless when he could just separate each line from the text file... it's so much easier to put new lines then quotes and commas round each one.

  6. #6
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default

    Thanks both. Any cleaner way to do it using the first method?
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


  7. #7
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Quote Originally Posted by Jahova View Post
    Thanks both. Any cleaner way to do it using the first method?
    You don't have to seed the RNG if you're using above PHP 4.2 I think?

  8. #8
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default

    Quote Originally Posted by Dentafrice View Post
    You don't have to seed the RNG if you're using above PHP 4.2 I think?
    English would be brilliant Maybe an example? Thanks
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


  9. #9
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    PHP Code:
    <?php

    function random_quote($data) {
        
    $random_number rand(0count($data) - 1);
        return 
    $data[$random_number];
    }

    $filename "lyrics.txt"// path to lyrics file
    $data file($filename); // gets each line of a file and puts it into an array.

    echo random_quote($data);
    echo 
    random_quote($data);
    ?>
    That lets you display a random quote each time you call random_quote.

  10. #10
    Join Date
    Nov 2005
    Posts
    4,486
    Tokens
    921

    Latest Awards:

    Default

    Thanks for your time, all. +REPPED
    "RETIRED" FROM HABBO(X)

    :¬:

    TOMSPIT / COWLY05


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •