php help needed!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Donners
    Confirmed User
    • Jun 2004
    • 689

    #1

    php help needed!

    I have this simple php script..

    Code:
    <?
       $quoteFile = "quotes.txt";  //File holding qoutes
    
       $fp = fopen($quoteFile, "r");   //Opens file for read
       $content = fread($fp, filesize($quoteFile));
       $quotes = explode("\n",$content);   //Put quotes into array
       fclose($fp);   //Close the file
    
       srand((double)microtime()*1000000);  // randomize
       $index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute
    
       echo $quotes[$index]; //Print quote to screen
    
    ?>
    But it only displays ONE qoute, I want it to display any number of qoutes of my choice. How can I do this?
  • Ice
    Confirmed User
    • Nov 2002
    • 26053

    #2
    are all your quotes saved in the quotes.txt file and is that file in the same directory as the random quote file?
    icq 1904905

    Comment

    • Donners
      Confirmed User
      • Jun 2004
      • 689

      #3
      Yes, and it has one qoute per line.

      Comment

      • Ice
        Confirmed User
        • Nov 2002
        • 26053

        #4
        Originally posted by Donners
        Yes, and it has one qoute per line.
        hmmm I just set it up on my server and it works fine....

        below is the code I used and an example of the quotes.txt file...

        index.php

        <html>
        <head>
        <title>Untitled</title>
        </head>

        <body>

        <?
        $quoteFile = "quotes.txt"; //File holding qoutes

        $fp = fopen($quoteFile, "r"); //Opens file for read
        $content = fread($fp, filesize($quoteFile));
        $quotes = explode("\n",$content); //Put quotes into array
        fclose($fp); //Close the file

        srand((double)microtime()*1000000); // randomize
        $index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute

        echo $quotes[$index]; //Print quote to screen
        ?>


        </body>
        </html>


        quotes.txt

        lick my balls
        eat my ass
        I like paste



        link to working example of above...

        http://www.pornfinders.com/quote/index.php
        icq 1904905

        Comment

        • Donners
          Confirmed User
          • Jun 2004
          • 689

          #5
          It works for me too. Maybe my question wasn't clear but I want to be able to show more then one qoute.

          Instead of getting

          "lick my balls"

          I want to get

          "lick my balls"
          "eat my ass"

          Or all three qoute if i want to. Any way of specificing how many qoutes it shows?

          Comment

          • Rice_Master
            Confirmed User
            • Mar 2001
            • 1582

            #6
            Code:
            <?
               $howmany = 3; // The number of quotes to display.
               $quoteFile = "quotes.txt";  //File holding qoutes
            
               $fp = fopen($quoteFile, "r");   //Opens file for read
               $content = fread($fp, filesize($quoteFile));
               $quotes = explode("\n",$content);   //Put quotes into array
               fclose($fp);   //Close the file
            
               For ($i = 0; $i < $howmany; $i++) {
             
               srand((double)microtime()*1000000);  // randomize
               $index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute
            
               echo $quotes[$index] . "<br>"; //Print quote to screen
            
               }
            
            ?>
            That'll work.
            Icq: 31706665

            Comment

            • Rice_Master
              Confirmed User
              • Mar 2001
              • 1582

              #7
              Originally posted by Rice_Master
              Code:
              <?
                 $howmany = 3; // The number of quotes to display.
                 $quoteFile = "quotes.txt";  //File holding qoutes
              
                 $fp = fopen($quoteFile, "r");   //Opens file for read
                 $content = fread($fp, filesize($quoteFile));
                 $quotes = explode("\n",$content);   //Put quotes into array
                 fclose($fp);   //Close the file
              
                 For ($i = 0; $i < $howmany; $i++) {
               
                 srand((double)microtime()*1000000);  // randomize
                 $index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute
              
                 echo $quotes[$index] . "<br>"; //Print quote to screen
              
                 }
              
              ?>
              That'll work.
              There's a chance of getting the same quote twice with that code though.
              Icq: 31706665

              Comment

              • Donners
                Confirmed User
                • Jun 2004
                • 689

                #8
                Many thanks!
                Works perfectly!
                Should have gone here directly, would have saved me a few hours of googling without any results.
                Thanks again, you saved my day

                Comment

                • swedguy
                  Confirmed User
                  • Jan 2002
                  • 7981

                  #9
                  Or a shorter version....

                  Code:
                  <?
                  $howmany = 3;
                  $quoteFile = "quotes.txt";
                  $quotesArr = file($quoteFile);
                  $quotes = array_rand($quotesArr, $howmany);
                  print join("<br>", $quotes);
                  ?>

                  Comment

                  • Donners
                    Confirmed User
                    • Jun 2004
                    • 689

                    #10
                    That only shows a bunch of numbers..

                    Comment

                    • swedguy
                      Confirmed User
                      • Jan 2002
                      • 7981

                      #11
                      Oops. Fast and wrong ;)
                      I forgot that it returned the keys and not the values. This one works:

                      Code:
                      <?
                      $howmany = 3;
                      $quoteFile = "in.log";
                      $quotesArr = file($quoteFile);
                      $keys = array_rand($quotesArr, $howmany);
                      foreach ($keys as $key)
                      	print $quotesArr[$key]."<br>";
                      ?>

                      Comment

                      Working...