1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Image rotation

Discussion in 'PHP' started by buckmajor, Dec 20, 2009.

  1. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #21
    Hey guys

    I have been using this one, but sometimes when I load a page the banner image shows nothing but blank. But then when I load another page, then it shows the image.

    <?php
    
    /**********************
    * Image Rotater
    ***********************
    * Written by Joel Larson (Coded Caffeine)
    * http://thejoellarson.com/
    *
    * Allows a person to add images with descriptions
    *   and pick an image randomly from a list.
    **********************/
    # Defined variables from paranoia.
    $images = array();
    $output = '';
    /*********************/
    
    # Copy/paste as many as you need.
    # There are two shown for an example! (Make sure they are unique!)
    $images[] = array(
        'path' => 'URL',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'DESCRIPTION'
    );
    
    $images[] = array(
        'path' => 'URL',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'DESCRIPTION'
    );
    
    # Set the int for total images in the first array level.
    $total_images = count($images);
    
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, $total_images);
    
    /**
    * Set the output that's being generated.
    */
    
    # If a path is defined..
    if( ! empty($images[$id]['path']))
    {
        # Create the image with path.
       $output =  '<img src="'.$images[$id]['path'].'" ';
       
        # If the alt text is defined, add it.
       if( ! empty($images[$id]['alternate']))
            $output .= 'alt="'.$images[$id]['alternate'].'" ';
       
        # If the title text is defined, add it.
       if( ! empty($images[$id]['title']))
            $output .= 'title="" ';
    
        $output .= '/>';
    }
    
    # Echo the result.
    echo $output;
    
    ?>
    HTML:
    Am I missing something here?
     
    buckmajor, Jan 18, 2010 IP
  2. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #22
    Give the example of how you're using the source and I'll be happy to help. :)
     
    CodedCaffeine, Jan 18, 2010 IP
  3. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #23
    Ok thanks CodedCaffeine,

    Here it is:
    <div id="banner">
        	<strong>Title</strong>
            <?php
    
    /**********************
    * Image Rotater
    ***********************
    * Written by Joel Larson (Coded Caffeine)
    * http://thejoellarson.com/
    *
    * Allows a person to add images with descriptions
    *   and pick an image randomly from a list.
    **********************/
    # Defined variables from paranoia.
    $images = array();
    $output = '';
    /*********************/
    
    # Copy/paste as many as you need.
    # There are two shown for an example! (Make sure they are unique!)
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-home.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Contact'
    );
    
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-bio.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Mark Lowdnes biography'
    );
    
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-gallery.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Video'
    );
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-blog.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Home'
    );
    
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-video.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Biography'
    );
    
    $images[] = array(
        'path' => 'http://www.test.com/wp-content/themes/mark/images/bg-contact.jpg',
        'alternate' => 'ALTERNATE TEXT',
        'title' => 'Gallery'
    );
    
    # Set the int for total images in the first array level.
    $total_images = count($images);
    
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, $total_images);
    
    /**
    * Set the output that's being generated.
    */
    
    # If a path is defined..
    if( ! empty($images[$id]['path']))
    {
        # Create the image with path.
        $output =  '<img src="'.$images[$id]['path'].'" ';
       
        # If the alt text is defined, add it.
        if( ! empty($images[$id]['alternate']))
            $output .= 'alt="'.$images[$id]['alternate'].'" ';
       
        # If the title text is defined, add it.
        if( ! empty($images[$id]['title']))
            $output .= 'title="" ';
    
        $output .= '/>';
    }
    
    # Echo the result.
    echo $output;
    ?>
      </div>
    Code (markup):
    I am using this 'Image Rotation' inside my Wordpress site on the header.php file, hope that doesn't have any effect on it??
     
    buckmajor, Jan 19, 2010 IP
  4. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #24
    Hah, had a bug in the script. :p

    Change this line:
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, $total_images);
    
    PHP:
    To this:
    # Using mt_rand because it's faster than rand().
    $id = mt_rand(0, --$total_images);
    PHP:
     
    CodedCaffeine, Jan 19, 2010 IP
  5. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #25
    YES!! It works perfectly. Every time I press the Reload or Refresh button the image changes without stumbling across a blank page. Thanks heaps CodedCaffeine.

    CHEERS ;)
     
    buckmajor, Jan 19, 2010 IP
  6. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #26
    My counter was a little bit off.. hah.

    When you use count() with an array, it counts the members of the array (Which say returned (int)6 ). The array itself can only be accessed with using $image[$id] with the $id being 0-5 (which is also (int)6 ). What my code did was search from 0-6, when there was only 5 images, so by reducing the value of the total images by 1, it will access it properly. :)
     
    CodedCaffeine, Jan 19, 2010 IP
  7. buckmajor

    buckmajor Active Member

    Messages:
    574
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #27
    I see now why there was an extra blank page. This is very handy, I'm beginning like PHP more and more ;)
     
    buckmajor, Jan 20, 2010 IP
  8. goliath

    goliath Active Member

    Messages:
    308
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #28
    Yeah, it's an index issue. An array populated in this way has an 0-based index. So here's what heppens in the code I quoted:

    The array is populated with two images.

    The number of images (2) is retrieved.

    A random ID between 0 and 2 is generated (here is your problem).

    The random ID is used to access the array, which has elements of 0 and 1. So if your random number between 0 and 2 was 2, you don't see any image.

    It doesn't matter if you have 2 images or 200, you can always generate an ID that's one higher than the actual number of IDs used in the array.

    The easy fix is to update your random number generation like this:

    
    
    $id = mt_rand(0, $total_images - 1);
    
    
    PHP:
     
    Last edited: Jan 20, 2010
    goliath, Jan 20, 2010 IP