Reading PHP file names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DanH87
    New Member
    • Jun 2008
    • 1

    Reading PHP file names

    Hi,

    I am currently having a bit of a problem in work with a PHP code. Here is my code:

    Code:
    <?php
    
    $nrandom1 = rand(1,3);
    $image_name1 = "image01.jpg";
    
    if ($nrandom1 == 2) {
    $image_name1 = "image01.jpg";
    }
    if ($nrandom1 == 3) {
    $image_name1 = "image02.jpg";
    }
    
    $nrandom2 = rand(1,3);
    $image_name2 = "image03.jpg";
    
    if ($nrandom2 == 2) {
    $image_name2 = "image03.jpg";
    }
    if ($nrandom2 == 3) {
    $image_name2 = "image04.jpg";
    }
    
    $nrandom3 = rand(1,3);
    $image_name3 = "image05.jpg";
    
    if ($nrandom3 == 2) {
    $image_name3 = "image05.jpg";
    }
    if ($nrandom3 == 3) {
    $image_name3 = "image06.jpg";
    }
    
    $nrandom4 = rand(1,3);
    $image_name4 = "image07.jpg";
    
    if ($nrandom4 == 2) {
    $image_name4 = "image07.jpg";
    }
    if ($nrandom4 == 3) {
    $image_name4 = "image08.jpg";
    }
    
    $nrandom5 = rand(1,3);
    $image_name5 = "image09.jpg";
    
    if ($nrandom5 == 2) {
    $image_name5 = "image09.jpg";
    }
    if ($nrandom5 == 3) {
    $image_name5 = "image10.jpg";
    }
    
    $nrandom6 = rand(1,3);
    $image_name6 = "image11.jpg";
    
    if ($nrandom6 == 2) {
    $image_name6 = "image11.jpg";
    }
    if ($nrandom6 == 3) {
    $image_name6 = "image12.jpg";
    }
    
    $nrandom7 = rand(1,3);
    $image_name7 = "image13.jpg";
    
    if ($nrandom7 == 2) {
    $image_name7 = "image13.jpg";
    }
    if ($nrandom7 == 3) {
    $image_name7 = "image14.jpg";
    }
    
    $nrandom8 = rand(1,3);
    $image_name8 = "image15.jpg";
    
    if ($nrandom8 == 2) {
    $image_name8 = "image15.jpg";
    }
    
    if ($nrandom8 == 3) {
    $image_name8 = "image16.jpg";
    }
    
    if ...
    {
        echo "<img src=\"logo.bmp\" border=\"1\" /><img src=\"$image_name5\" border=\"1\" /><img src=\"$image_name6\" border=\"1\" /><img src=\"$image_name7\" border=\"1\" /><img src=\"$image_name8\" border=\"1\" />";
    }
    else
    {
        echo "<img src=\"logo.bmp\" border=\"1\" /><img src=\"$image_name1\" border=\"1\" /><img src=\"$image_name2\" border=\"1\" /><img src=\"$image_name3\" border=\"1\" /><img src=\"$image_name4\" border=\"1\" />";
    }
    
    
    ?>
    This code is incomplete because I need to insert some sort of variable at the top and also complete the if statement on line 84. At first, I had four images on a banner, each of which were picked by a random generator. Each generator has a choice of two images to pick for that particular space.

    However, I have since created more random generators ($nrandom5 - $nrandom8). These work in exactly the same way as the first four did, but what I want to do is to be able to load images from $nrandom5 to $nrandom8 on one page, but images from $nrandom1 to $nrandom4 on the other pages.

    Basically, I think I need my code to say "if (name of php file), display $image_name5 to $image_name8, else display $image_name1 to $image_name4". My problem is I am not sure how to get PHP to recognise the names of other PHP files. It is probably a simple function but I am relatively inexperienced in PHP and so any help would be appreciated.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    You say this code is beeing used in several pages. How is it beeing called?
    Is it beeing included?
    You can use the __LINE__ constant to get the fill path of the file beeing executed.
    Using basename(__LINE __) will get you the file name.

    You could try something like:
    [code=php]
    function print_random_im ages($imgStartI ndex, $numImages) {
    for($x = 0, $y = $imgStartIndex; $x < $numImages; $x++, $y += 2)
    {
    echo "<img src='image". ($y + mt_rand(0, 1)) .".jpeg' />\n";
    }
    }
    [/code]
    I use the for loop there to get rid of all the if statements you use.

    Then, if you put this function into a file and include it into the files you want you images show on, you can use it like so:
    [code=php]
    # From the first file
    include("my_ima ge_function.php ");
    print_random_im ages(1, 4);

    # Fromt any other file
    include("my_ima ge_function.php ");
    print_random_im ages(9, 4);
    [/code]

    Comment

    Working...