New Image (Each Time) Page Is Loaded

Discussion in 'PHP' started by LindseyInteractive, Apr 26, 2011.

  1. #1
    Hello,

    I have a site I am running, and I am looking for a quick PHP script/code that will allow one of two images to load each time the page is loaded.

    For Examples, I have 2 Picture, lets call them picture1.jpg and picture2.jpg.

    The first time the page is loaded picture1.jpg loads, and then the next time the page is loaded, picture2.jpg loads.

    Please help me with this.

    Thanks in advance.
     
    LindseyInteractive, Apr 26, 2011 IP
  2. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #2
    One really simple solution is to use sessions to store which picture was displayed previously. It increases the value in the session each time the page is loaded. If this number is odd, it displays the picture1.jpg and if the number is even, it displays the picture2.jpg.

    
    <?php
    
    session_start();
    
    function is_odd( $number ) {
    	
       return $number & 1 == 1 ? true : false;
    }
    
    $_SESSION['pic'] = (isset($_SESSION['pic']) && !empty($_SESSION['pic'])) ? $_SESSION['pic'] + 1 : 1;
    
    if ( is_odd($_SESSION['pic']) ) 
    	$picture = 'picture1.jpg';
    else 
    	$picture = 'picture2.jpg';
    
    echo $picture;
    
    
    PHP:
    If you want to add more than two pictures, you can still use sessions and I would use switch statement.

    
    <?php
    
    session_start();
    
    if ( isset($_SESSION['pic']) && !empty($_SESSION['pic']) ) {
    	
    	switch ( $_SESSION['pic'] ) {
    		
    		case 1 :
    			$picture         = 'picture1.jpg';
    			$_SESSION['pic'] = 2;
    			break;
    		case 2 :
    			$picture         = 'picture2.jpg';
    			$_SESSION['pic'] = 3;
    			break;
    		case 3 :
    			$picture         = 'picture3.jpg';
    			$_SESSION['pic'] = 1;
    			break;
    		default :
    			$picture         = 'picture1.jpg';
    			$_SESSION['pic'] = 2;
    			break;
    	}
    }
    else {
    	
    	$picture         = 'picture1.jpg';
    	$_SESSION['pic'] = 2;
    }
    
    echo $picture;
    
    
    PHP:
    I hope it helps ;)
     
    bogi, Apr 26, 2011 IP
  3. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    $images = array(
        "/path/to/images/image1.jpg",
        "/path/to/images/image2.jpg",
        /*
         .
         .
         .
         more images if necessary
         .
         .
         .
         */
    );
    array_shuffle($images); // shuffles the array randomly just the way you shuffle a deck of cards
    $image = $images[0];
    echo '<img src="' . $image . '" />';
    ?>
    
    PHP:
     
    ap2010, Apr 27, 2011 IP
  4. arjunskumar47

    arjunskumar47 Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #4
    Arrange image in numerical order eg: 1.jpg,
    2.jpg,
    3.jpg,
    ..
    n.jpg.
    
    <?php
    $img_url = "http://domain/image_directory";
    // Without trailing slash
    $rand_img_num = rand(1,n);
    // rand function selects a random number from the 1 to n list of numbers.
    $rand_image = $rand_img_num.'.jpg';
    $rand_img_url = $img_url.'/'.$rand_image ;
    echo $rand_img_url;
    ?>
    
    PHP:
    Hope that Helps
    Cheers!
    :)
     
    arjunskumar47, Apr 30, 2011 IP