Random include() help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • npm
    New Member
    • Apr 2007
    • 57

    Random include() help

    I have some code that will randomly load an image from an array, but I want to tweak it to be able to randomly choose a php file and "include()" it in my page.

    Here's the original code:

    Code:
    <?php
    $file_array = array
    ('http://www.samplesite.com/images/image1.jpg',
    'http://www.samplesite.com/images/image2.jpg',
    'http://www.samplesite.com/images/image3.jpg',
    'http://www.samplesite.com/images/image4.jpg',
    'http://www.samplesite.com/images/image5.jpg');
    $total = count($file_array);
    $random = (mt_rand()%$total);
    $file = "$file_array[$random]";
    print("<img src=$file></a>");
    ?>
    And here's my tweaked version that gave me an error:

    Code:
    <?php
    $file_array = array
    ('http://www.samplesite.com/samplefile1.php',
    'http://www.samplesite.com/samplefile2.php',
    'http://www.samplesite.com/samplefile3.php',
    'http://www.samplesite.com/samplefile4.php',
    'http://www.samplesite.com/samplefile5.php');
    $total = count($file_array);
    $random = (mt_rand()%$total);
    $file = "$file_array[$random]";
    include('$file');
    ?>
    I'm pretty new to php, so any help is greatly appreciated. Thanks in advance!
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi

    [PHP]<?php
    $file_array = array
    ('http://www.samplesite. com/samplefile1.php ',
    'http://www.samplesite. com/samplefile2.php ',
    'http://www.samplesite. com/samplefile3.php ',
    'http://www.samplesite. com/samplefile4.php ',
    'http://www.samplesite. com/samplefile5.php ');
    $total = count($file_arr ay);
    $random = (mt_rand()%$tot al);
    $file = $file_array[$random];
    include($file);
    ?>[/PHP]

    try that..

    Purple

    Comment

    Working...