Random include ?

Discussion in 'PHP' started by Lifestyle trader1, Jun 1, 2009.

  1. #1
    Hi i am trying to work out a Random Include script

    so if i use the script it will populate a div / section of my site

    from a list of files I have defined, any ideas ?
    switch statement trickery?
     
    Lifestyle trader1, Jun 1, 2009 IP
  2. givemeknol

    givemeknol Active Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #2
    <?php
    $files = array("file1.php","file2.php","file3.php");
    $selected_file = $files[rand(0,count($files)-1)];
    include($selected_file);
    ?>
     
    givemeknol, Jun 1, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    I would use mt_rand, according to php.net it is a faster algorithm
     
    Kaizoku, Jun 1, 2009 IP
  4. sniurkst

    sniurkst Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or you can use array_rand function : array_rand ($file_array, $numberofitemstoget)

    <?php
    $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input, 2);
    echo $input[$rand_keys[0]] . "\n";
    echo $input[$rand_keys[1]] . "\n";
    ?>
    Code (markup):
     
    sniurkst, Jun 2, 2009 IP