SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: randomizing

  1. #1
    SitePoint Addict
    Join Date
    Oct 2001
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    randomizing

    I've got a list of quotes in a text file, what's the best way to output a single random quote from that list onto a webpage?
    Would I keep it in the text file or input them in a mySQL database? ..and how would I go about doing the PHP for that

    Thanks alot for any advice

  2. #2
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $quotes=file("quotes.txt");

    $number_quotes=count($quotes);
    if(
    $number_quotes!==0){$number_quotes.=-1;};
    $random=rand(0,$number_quotes);
    echo 
    $quotes[$random]; 
    thats what i use. You just keep all your quotes ina text file with one quote per line
    hmm...

  3. #3
    SitePoint Addict
    Join Date
    Oct 2001
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works a charm

    Just out of curiosity what's the .= mean?

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by random
    Just out of curiosity what's the .= mean?
    i don't know why that's in there. that adds text onto the end of a STRING. i'm surprised it works like that. there's an extra ";" in there too. this is how i would do it:

    PHP Code:
    $quotes file("quotes.txt");
    $number_quotes count($quotes);

    if (
    $number_quotes) { $number_quotes -= 1; }
    $random rand(0$number_quotes);
    echo 
    $quotes[$random]; 
    Last edited by DR_LaRRY_PEpPeR; Oct 14, 2001 at 21:09.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


Bookmarks

Posting Permissions

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