I'm a newbie to PHP, I'm trying to learn all I can. I build webpages and so far I've only been using HTML and CSS. But I was introduced to PHP and all the awesome stuff it can do. But anyway. What I want to figure out how to do, is make random quotes appear on the site. I have a .txt file of all these quotes by famous people I want to appear randomly each day. Like today would be a quote by George Washington, the next a quote by Mark Twain. Stuff like that. Can anyone help me?

-Lucky

    I found this using google.ca, credit not mine. It is much more efficient then my version 🙂

    /*
     J at TIPPELL dot com
    12-Jun-2004 10:18
    heres a little script to return a random quote from a quotes file. */
    
    <?php
    $textfile = "Includes/Quotes.txt";      //quotes file
    if ($quotes = @file("$textfile")) {     //don't display errors on file open
       $quote = rand(0, sizeof($quotes)-1);
       echo $quotes[$quote];                //echo a random quote
    }else{
       echo ("default quote");           //if quotes file wasn't found, echo out a default quote
    }
    ?>
    

      Even a little bit more efficient:

      <?php
      $textfile = "Includes/Quotes.txt";      //quotes file
      if ($quotes = @file("$textfile")) {     //don't display errors on file open
         echo $quotes[array_rand($quotes)];   //echo a random quote
      }else{
         echo ("default quote");           //if quotes file wasn't found, echo out a default quote
      }
      ?>
      

        I appreciate all the help. If you find anything else, let me know. Thanks again!

        -Lucky

          set the permisions on your txt file to 777.
          <?php
          $file = 'nameof file.txt';
          $array = file($file);
          shuffle($array);
          echo "$array[0]";
          ?>

            Write a Reply...