Diplay random text line

Discussion in 'PHP' started by bondigor69, Dec 27, 2012.

  1. #1
    hey guys I have few lines like the example below

    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>
    <a href="link"><img src="link"></a>

    With php I would like to choose random 5 lines and display them.

    Help will be appreciated. Thank you
     
    bondigor69, Dec 27, 2012 IP
  2. yelbom

    yelbom Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Put your links in a txt file called links.txt and run the code below

    
    <?php
    $dirs = file("links.txt");
        shuffle($dirs);
        $folders = explode("\n", $dirs[0]);
        $dir_folder = $folders[0];
        echo $dir_folder;
    ?>
    
    PHP:
     
    yelbom, Dec 27, 2012 IP
  3. ogah

    ogah Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    
    <?php
    $dirs = file("links.txt");
    shuffle($dirs);
    for ($i = 0; $i < 5; $i++) {
    echo $dirs[$i];
    }
    ?>
    Code (markup):
     
    ogah, Dec 27, 2012 IP
  4. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #4
    I would not use a file to do this - I would use an array that you preset or populate with a database.

    Example of Array

    I would recommend to populate with a database - as it would be much more simple to just to ORDER BY rand in the query

    Array Way

    
    <?php
    
      $array_of_links = array(
                                          array('url' => 'http://www.myurl.com/1/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'),
                                          array('url' => 'http://www.myurl.com/2/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'),                                 
                                          array('url' => 'http://www.myurl.com/3/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'),                                 
                                          array('url' => 'http://www.myurl.com/4/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg'),                                 
                                          array('url' => 'http://www.myurl.com/5/', 'anchor' => 'Anchor Text', 'image_src' => 'http://.../image.jpg')                                 
    
    );
    
      // next all you have to do is just get random
      $randomized_links = shuffle($array_of_links);
    
      // to use them
      foreach( $randomized_links as $link ):
          
         echo "<a href='" . $link['url'] . "'>" . "<img src='" . $link['image_src'] . "' alt='" . $link['anchor'] . "'>" . "</a>";  
      
      endforeach;
    ?>
    
    Code (markup):
    Note: This Code May Break I didn't test the code - but you should get the general idea from it.
     
    FCM, Dec 31, 2012 IP