SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Lincoln
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Random Images?

    Is there a way with php to display multiple random images on one page? I realise it's possible using javascript, but I'm avoiding js like the plague. I would like to display about 5 or so random thumbnails on my right-hand menu, and have each picture link to it's respected large sized version. Is there a way of doing this fairly easily?

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Lincoln
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no ideas?

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this?

    The $images array should of course get populated from your thumbnails and images directory.
    The constant RANDOM_IMAGES_COUNT specifies the numer of random images to display.
    You will probably have to change the HTML, see the echo call.
    PHP Code:
    <?php
    $images 
    = array('My Testpicture' => array('test_small.jpg''test_big.jpg'),
                    
    'Foobar'         => array('foo_small.jpg''foo_big.jpg'));

    define('RANDOM_IMAGES_COUNT'1);

    if (
    count($images) >= RANDOM_IMAGES_COUNT) {
        
    $keys array_keys($images);
        
    $random_images = array();
        for (
    $i 1$i <= RANDOM_IMAGES_COUNT$i++) {
            while (
    array_key_exists($rand rand(0count($images)-1), $random_images)) { };
            
    $random_images[$keys[$rand]] = $images[$keys[$rand]];
        }
        foreach (
    $random_images as $title => $fnames) {
            echo 
    '<a href="'.$fnames[1].'"><img src="'.$fnames[0].'" title="'.$title.'" alt="'.$title.'" /></a>';
        }
    } else {
        die(
    'Not enough images');
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Lincoln
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ever so much, I'll give this a whirl!

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Lincoln
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, works a treat!!!!


    Thanks so much!!

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad it works for you. Merry Christmas Benny Venables.


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
  •