Advanced Search

Results 1 to 6 of 6

Thread: PHP randomizer show file and filename

  1. #1
    Join Date
    May 2008
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default PHP randomizer show file and filename

    I'm looking for a script that will allow me to randomize and show both an image and its name (without the .gif, .jpg. etc.). I already know how to use a PHP randomizer to show/pull images from a single directory, but I was wondering if it was possible to also show its filename at the same time without extensions i.e.

    (pretend) actual randomized images: image01.gif image02.gif image03.gif
    and then right beneath in text or wherever else I wish to place it: image01, image02, image03

    I've seen some alternatives using arrays i.e. mostly banner/advertisement rotations, however I have over 5000 images I wish to rotate and don't want to enter them in manually.

    Thanks for the help!

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Use techietims solution all the way down, mines terrible
    Here you go, explanation below:
    PHP Code:
    <?php
    $imagesNum 
    8//the amount of images.
    $random rand(1,$imagesNum);
    $data = array('image','png'); //array('prefix','type')
    $imageDisplay $data[0].$random.chr(46).$data[1];
    $imageName substr($imageDisplay0strpos($imageDisplay,$data[1])-1);
    echo 
    "<img src='$imageDisplay' /><br />$imageName";
    ?>
    Ok, starting with the first line:
    PHP Code:
    $imagesNum 8//the amount of images. 
    This is the amount of images you have in the file
    PHP Code:
    $random rand(1,$imagesNum); 
    Leave this line as it is.
    PHP Code:
    $data = array('image','png'); //array('prefix','type') 
    The first section where it says image, is what the prefix is, so image1 to image $imagesNum.
    And the second section is the type, so above you said gif, so change it to gif.
    And rest:
    PHP Code:
    $imageDisplay $data[0].$random.chr(46).$data[1];
    $imageName substr($imageDisplay0strpos($imageDisplay,$data[1])-1);
    echo 
    "<img src='$imageDisplay' /><br />$imageName"
    Leave it alone. In the image element though, if your using standard HTML, take away the back slash.

    I hope this helps.
    Last edited by Nile; 07-24-2008 at 11:25 PM.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    Feldt (07-24-2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Hmm, that's quite hefty nile.

    Give this a go:
    PHP Code:
    <?php
    $imgs 
    array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's 
    $key array_rand($imgs);
    echo 
    '<img src="' $imgs[$key] . '" /><br />' substr($imgs[$key], 0, -4);

  5. The Following User Says Thank You to techietim For This Useful Post:

    Feldt (07-24-2008)

  6. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    whoops. here you go:
    PHP Code:
    <?php
    $imgs 
    array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's 
    $key array_rand($imgs5);
    foreach(
    $key as $i)
      echo 
    '<img src="' $imgs[$i] . '" /><br />' substr($imgs[$i], 0, -4) . '<br />';

  7. #5
    Join Date
    May 2008
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks both (Nile and techietim) for the quick responses =) Both work and thanks for taking the time to write them out.

    edit// thanks techietim for the edit =)

    Whoops one last question: would it be possible to make it so that the images and text can be on separate lines, like in the above example I gave

    i.e.
    image, image, image, image, image
    < br > name, name, name, name, name
    Last edited by Feldt; 07-24-2008 at 11:33 PM.

  8. #6
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    <?php
    $imgs 
    array_merge(glob('*.gif'), glob('*.jpg'), glob('*.png')); //GIF's, JPG's and PNG's 
    $key array_rand($imgs5);
    $nImgs = array();
    $nCaptions = array();
    foreach(
    $key as $i){
      
    $nImgs[] = $imgs[$i];
      
    $nCaptions[] = substr($imgs[$i], 0, -4);
    }
    echo 
    '<table border="0">
    <tr>'
    ;
    foreach(
    $nImgs as $i)
      echo 
    '<td style="text-align:center"><img src="' $i '" /></td>';
    echo 
    '</tr><tr>';
    foreach(
    $nCaptions as $i)
      echo 
    '<td style="text-align:center">'.$i.'</td>';
    echo 
    '</table>';

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •