PHP: Displaying random lines from text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eMonk
    Confirmed User
    • Aug 2003
    • 2310

    #1

    PHP: Displaying random lines from text file

    I want to display thumb links randomly from a text file in php. here's a sample link:

    Code:
    <a href="../models/samantha/" class="tip thumb"><img src="../models/samantha/thumb.jpg" width="88" height="88"><span>Samantha</span></a>
    text file will have a max of 50 lines of so. this is a temp solution until i start playing with mysql. right now im looking for a pure php solution. how can this be done?
  • psili
    Confirmed User
    • Apr 2003
    • 5526

    #2
    <?php
    echo array_rand(file('../path/to/text.txt'));
    ?>

    shit.
    i suck.
    thus a bump for you.
    Your post count means nothing.

    Comment

    • NickSunshine
      Confirmed User
      • Mar 2006
      • 1196

      #3
      PHP Code:
      <?
      $a=file('filename.txt');
      $b=array_rand($a);
      print($a[$b]);
      ?>

      Comment

      • eMonk
        Confirmed User
        • Aug 2003
        • 2310

        #4
        Originally posted by NickSunshine
        PHP Code:
        <?
        $a=file('filename.txt');
        $b=array_rand($a);
        print($a[$b]);
        ?>

        Error: Warning: array_rand() [function.array-rand]: First argument has to be an array.

        Do I need anything special in my text file?

        Comment

        • NickSunshine
          Confirmed User
          • Mar 2006
          • 1196

          #5
          Originally posted by eMonk
          Error: Warning: array_rand() [function.array-rand]: First argument has to be an array.

          Do I need anything special in my text file?
          sounds like it isn't reading the text file at all.

          is the path correct?

          try adding a print_r($a); after the file function to see whats in there.

          Comment

          • eMonk
            Confirmed User
            • Aug 2003
            • 2310

            #6
            after adding print_r($a); it displays the thumb link but the error still remains... any ideas?

            Comment

            • NickSunshine
              Confirmed User
              • Mar 2006
              • 1196

              #7
              sorry to ask:

              is there a $ next to the a in the array_rand() function?

              $b=array_rand($a);

              short of that, no- i have no idea.

              Comment

              • psili
                Confirmed User
                • Apr 2003
                • 5526

                #8
                Are you editing your text file of random lines on a windows box and pushing it to a unix box or vice-versa? Maybe your text file's all torqued up. Try something simple like just a line of:

                a
                b
                c
                d
                e
                f

                and see what happens.
                Your post count means nothing.

                Comment

                • eMonk
                  Confirmed User
                  • Aug 2003
                  • 2310

                  #9
                  Originally posted by psili
                  Are you editing your text file of random lines on a windows box and pushing it to a unix box or vice-versa? Maybe your text file's all torqued up. Try something simple like just a line of:

                  a
                  b
                  c
                  d
                  e
                  f

                  and see what happens.

                  that worked but it's only displaying 1 letter at a time.. i want all displayed randomly side-by-side. why doesn't the txt like my a href links?

                  Comment

                  • fris
                    Too lazy to set a custom title
                    • Aug 2002
                    • 55679

                    #10
                    Code:
                    <?
                    
                    // urls.txt should be format of <a href="domain.com"><img src="domain.jpg"></a>
                    // 1 per line
                    
                    $file = "urls.txt";
                    $fp = file($file);
                    srand((double)microtime()*1000000);
                    $urls = $fp[array_rand($fp)];
                    echo $urls;
                    
                    ?>
                    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

                    Comment

                    • NickSunshine
                      Confirmed User
                      • Mar 2006
                      • 1196

                      #11
                      Originally posted by eMonk
                      that worked but it's only displaying 1 letter at a time.. i want all displayed randomly side-by-side. why doesn't the txt like my a href links?
                      this is my fault for not completely reading your first post.

                      for some reason i thought you only wanted one line...

                      PHP Code:
                      <?
                      
                      $a=file('textfile.txt');
                      array_shuffle($a);
                      for each($a as $line){
                         print("<a href='../models/" . $line . "'><img src='../models/" . $line . "/thumb.jpg' />" . ucwords($line) . "</a>");
                      }
                      
                      ?>
                      Last edited by NickSunshine; 12-23-2008, 05:41 PM. Reason: <? ?>

                      Comment

                      • eMonk
                        Confirmed User
                        • Aug 2003
                        • 2310

                        #12
                        Originally posted by fris
                        Code:
                        <?
                        
                        // urls.txt should be format of <a href="domain.com"><img src="domain.jpg"></a>
                        // 1 per line
                        
                        $file = "urls.txt";
                        $fp = file($file);
                        srand((double)microtime()*1000000);
                        $urls = $fp[array_rand($fp)];
                        echo $urls;
                        
                        ?>

                        displays one thumb at a time. how can we display them ALL side-by-side randomly?

                        Comment

                        • eMonk
                          Confirmed User
                          • Aug 2003
                          • 2310

                          #13
                          Originally posted by NickSunshine
                          this is my fault for not completely reading your first post.

                          for some reason i thought you only wanted one line...

                          PHP Code:
                          <?
                          
                          $a=file('textfile.txt');
                          array_shuffle($a);
                          for each($a as $line){
                             print("<a href='../models/" . $line . "'><img src='../models/" . $line . "/thumb.jpg' />" . ucwords($line) . "</a>");
                          }
                          
                          ?>

                          what should the txt file look like?

                          samantha thumb.jpg text here?

                          Comment

                          • psili
                            Confirmed User
                            • Apr 2003
                            • 5526

                            #14
                            Originally posted by eMonk
                            displays one thumb at a time. how can we display them ALL side-by-side randomly?
                            Not sure about why the HREF's aren't display but try:

                            $deck = file('test.txt');
                            shuffle($deck);
                            echo implode(' ',$deck);
                            Your post count means nothing.

                            Comment

                            • NickSunshine
                              Confirmed User
                              • Mar 2006
                              • 1196

                              #15
                              Originally posted by eMonk
                              what should the txt file look like?

                              samantha thumb.jpg text here?
                              samantha
                              jenny
                              debbie
                              susan
                              christy
                              ...
                              ...
                              julia

                              Comment

                              • eMonk
                                Confirmed User
                                • Aug 2003
                                • 2310

                                #16
                                Originally posted by psili
                                Not sure about why the HREF's aren't display but try:

                                $deck = file('test.txt');
                                shuffle($deck);
                                echo implode(' ',$deck);

                                That worked... thanks!... nice & short too...

                                Thanks to everyone else that helped aswell! Much appreciated!

                                I just hope this phrases fast when 50 or so entries are added to the txt file...

                                Comment

                                • NickSunshine
                                  Confirmed User
                                  • Mar 2006
                                  • 1196

                                  #17
                                  correction: use shuffle($a); not array_shuffle($a);

                                  i invented a function i guess :D

                                  Comment

                                  • psili
                                    Confirmed User
                                    • Apr 2003
                                    • 5526

                                    #18
                                    This eMonk thread should be pinned somewhere.

                                    It's a very simple question that had a pretty simple solution. However, the communication between ALL involved just wasn't "right" which is the reason it took a while to solve.

                                    There's a lesson here kids, and it's not about how to properly sniff your own ass crack, you crazy monkeys, you.
                                    Your post count means nothing.

                                    Comment

                                    • eMonk
                                      Confirmed User
                                      • Aug 2003
                                      • 2310

                                      #19
                                      thanks again... i'm still new to php... reading my way through, "PHP and MySQL Web Development 4th edition" which is a GREAT book btw, and I knew the method wouldn't be too much lines of code for this task. I also believe it's bad practice to use PHP to display HTML.

                                      Comment

                                      • NickSunshine
                                        Confirmed User
                                        • Mar 2006
                                        • 1196

                                        #20
                                        Originally posted by eMonk
                                        I also believe it's bad practice to use PHP to display HTML.
                                        in my opinion, it really depends on the situation.

                                        sometimes you just gotta loop.

                                        Comment

                                        • Bro Media - BANNED FOR LIFE
                                          MOBILE PORN: IMOBILEPORN
                                          • Jan 2004
                                          • 16502

                                          #21
                                          Easy peasy.

                                          Comment

                                          • Voodoo
                                            ♥ ♦ ♣ ♠
                                            • Sep 2002
                                            • 10600

                                            #22
                                            Code:
                                            <?php include("myrandomtextfile.txt"); ?>
                                            Then once a day or once a minute (Whatever you want), open myrandomtextfile.txt and replace the text in it randomly!

                                            "I'm selflessly supporting the common good, but only coincidentally looking out for No.1."

                                            Comment

                                            • eMonk
                                              Confirmed User
                                              • Aug 2003
                                              • 2310

                                              #23
                                              Originally posted by Voodoo
                                              Code:
                                              <?php include("myrandomtextfile.txt"); ?>
                                              Then once a day or once a minute (Whatever you want), open myrandomtextfile.txt and replace the text in it randomly!

                                              lol voodoo..

                                              Comment

                                              • psili
                                                Confirmed User
                                                • Apr 2003
                                                • 5526

                                                #24
                                                Originally posted by eMonk
                                                thanks again... i'm still new to php... reading my way through, "PHP and MySQL Web Development 4th edition" which is a GREAT book btw, and I knew the method wouldn't be too much lines of code for this task. I also believe it's bad practice to use PHP to display HTML.
                                                Dude, keep learning and asking questions. My last post was geared toward the train-wreck posts about how a client / designer / coder was fucked over because they just didn't communicate.

                                                If you get comfortable with a skill, can communicate with a client on how to apply that skill, and can get both you and a client to agree and understand terms on the skill you apply please let me know. :-)
                                                Your post count means nothing.

                                                Comment

                                                • NickSunshine
                                                  Confirmed User
                                                  • Mar 2006
                                                  • 1196

                                                  #25
                                                  Originally posted by psili
                                                  There's a lesson here kids, and it's not about how to properly sniff your own ass crack, you crazy monkeys, you.
                                                  monkey work

                                                  i learned a couple things...

                                                  a) read the first post COMPLETELY before you try to help.
                                                  b) array_shuffle() is not a function.
                                                  c) PHP and MySQL Web Development 4th edition is a GREAT book.

                                                  Comment

                                                  • psili
                                                    Confirmed User
                                                    • Apr 2003
                                                    • 5526

                                                    #26
                                                    Originally posted by NickSunshine
                                                    monkey work

                                                    i learned a couple things...

                                                    a) read the first post COMPLETELY before you try to help.
                                                    b) array_shuffle() is not a function.
                                                    c) PHP and MySQL Web Development 4th edition is a GREAT book.
                                                    I re-read the original post and still would go for a solution of outputting a random one line of a text file. That's why this thread is so perfect in it's ability to illustrate how communication is so important; for all parties.

                                                    Regardless. I like my crab cioppino served with warm garlic bread.
                                                    Huh?
                                                    Your post count means nothing.

                                                    Comment

                                                    • Bro Media - BANNED FOR LIFE
                                                      MOBILE PORN: IMOBILEPORN
                                                      • Jan 2004
                                                      • 16502

                                                      #27
                                                      Originally posted by psili
                                                      I re-read the original post and still would go for a solution of outputting a random one line of a text file. That's why this thread is so perfect in it's ability to illustrate how communication is so important; for all parties.

                                                      Regardless. I like my crab cioppino served with warm garlic bread.
                                                      Huh?
                                                      PHP Code:
                                                      <?php
                                                      $lines = file('yourfile.txt');
                                                      $num = count($lines);
                                                      $r = rand(1, $num)-1;
                                                      echo $lines[$r];
                                                      ?>

                                                      Comment

                                                      • eMonk
                                                        Confirmed User
                                                        • Aug 2003
                                                        • 2310

                                                        #28
                                                        Yeah, I could have been more clear in my original post. I never did mention to output ALL the links at once. Could have added more lines in the sample link aswell.

                                                        However this is great... GFY PHP Support Group!

                                                        Comment

                                                        • pornbling
                                                          Registered User
                                                          • Apr 2008
                                                          • 251

                                                          #29
                                                          try this:

                                                          $fp=file('yourfile.txt');
                                                          shuffle($fp);
                                                          echo $fp[0];

                                                          This should do it. If you want to see more
                                                          echo $fp[1]; echo $fp[2]; ...

                                                          Comment

                                                          • quantum-x
                                                            Confirmed User
                                                            • Feb 2002
                                                            • 6863

                                                            #30
                                                            OK.
                                                            Seeing as noone else has solved this yet. (IE, haven't used the 'FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES' flags, meaning output would be fubar)
                                                            PHP Code:
                                                            <?
                                                            $f = file('/path/to/names/names.txt',FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
                                                            shuffle($f);
                                                            foreach($f as $k => $v) echo '<a href="../models/'.$v.'/" class="tip thumb"><img src="../models/'.$v.'/thumb.jpg" width="88" height="88"><span>'.strtoupper($v[0]).substr($v,1).'</span></a>'."\n";
                                                            ?>
                                                            Renders:
                                                            Code:
                                                            <a href="../models/silvia/" class="tip thumb"><img src="../models/silvia/thumb.jpg" width="88" height="88"><span>Silvia</span></a>
                                                            <a href="../models/billy/" class="tip thumb"><img src="../models/billy/thumb.jpg" width="88" height="88"><span>Billy</span></a>
                                                            <a href="../models/jenna/" class="tip thumb"><img src="../models/jenna/thumb.jpg" width="88" height="88"><span>Jenna</span></a>
                                                            <a href="../models/adam/" class="tip thumb"><img src="../models/adam/thumb.jpg" width="88" height="88"><span>Adam</span></a>
                                                            With names capitalised as requested.
                                                            PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                                                            Comment

                                                            Working...