1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Rotating images on an HTML page ...

Discussion in 'General Programming Chat' started by OTrap, Mar 22, 2011.

  1. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,282
    Likes Received:
    1,051
    Suppose I have a .txt file of a thousand image URLs (we'll call the file image.txt), and I have a place on my site where I want those images displayed, but I want the image that displays to rotate through the list each time the page loads, so that each time the page loads, the next image down in the list displays in that spot.

    Is this possible, and if so, how would I go about that?
     
  2. GreyWolf

    GreyWolf Executive VIP Jr. VIP

    Joined:
    Aug 17, 2009
    Messages:
    1,930
    Likes Received:
    5,387
    Gender:
    Male
    Occupation:
    Artist / Craftsman
    Location:
    sitting at my PC
    OK. A couple questions so I know exactly what you're wanting to do.

    Does it need to display just a single image for each page load, or use that image as a start position for a slide show on the page?

    Does it have to display the images in order for each page load, or can it be just a random image each time?
     
  3. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Here's the code to display a totally random pic from the list (untested but it should work):

    PHP:
    <?php

    // Path to your file with your pic urls in it
    // One url per line
    $filename 'images.txt';

    // Load list of filenames
    $lines file($filename);
    foreach(
    $lines as $line) {
        
    $pics trim($line);
    }
    shuffle($pics);

    // Pick a random image
    $pic array_shift($pics);

    // Display pic
    echo "<img src=\"$pic\">\n";

    ?>
    If you want to show the pics in order then you have to set a cookie each session to keep track of what pic you're up to. Don't have time to write it out right now, but if you search my posts for "cookie" I posted something similar a while back.
     
  4. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Try this... It sets a session cookie to keep track of which pic you're up to, and loops through the pics from start to end, then starts at the beginning again.

    Code:
    <?php
    
    // Path to your file with your pic urls in it
    // One url per line
    $filename = 'images.txt';
    
    // Load list of filenames
    $lines = file($filename);
    foreach($lines as $line) {
    	$line = trim($line);
    	if(!empty($line)) {
    		$pics[] = $line;
    	}
    }
    
    // Max pic count
    $max = count($pics) - 1;
    
    // Set / increment the cookie value
    if(isset($_COOKIE['views'])) {
    	// Reset views if we reached end of $pics
    	// otherwise increment views by 1
    	if($_COOKIE['views'] >= $max) {
        	setcookie('views', 0);
    	} else {
    		$views = $_COOKIE['views'];
    		$views++;
        	setcookie('views', $views);
    	}
    } else {
        setcookie('views', 0);
    }
    
    // Which pic to display
    $p   = $_COOKIE['views'];
    $pic = $pics[$p];
    
    
    ?>
    <html>
    <head>
    <title>Blah blah</title>
    </head>
    <body>
    <?php
         echo "Cookie value: ". $_COOKIE['views'] ."<br>";
        // Show the fuckin pic
        echo "<img src=\"$pic\"><br>\n";
    ?>
    Blah blah blah
    </body>
    </html>
    
    Edit: just tweaked the script.
     
    • Thanks Thanks x 2
    Last edited: Mar 22, 2011
  5. GreyWolf

    GreyWolf Executive VIP Jr. VIP

    Joined:
    Aug 17, 2009
    Messages:
    1,930
    Likes Received:
    5,387
    Gender:
    Male
    Occupation:
    Artist / Craftsman
    Location:
    sitting at my PC
    Cool, the code Autumn gave looks like it should work. Just saved it for myself too. :cool:
     
    • Thanks Thanks x 2
  6. partymarty4870

    partymarty4870 Elite Member

    Joined:
    Jul 7, 2010
    Messages:
    2,034
    Likes Received:
    1,690
    Location:
    I come from a land downunder
    here's some code for rotating images if you're not working with php.

    I use it to rotate clickable banner ads but could be tweaked for what you need I guess.

    Code:
    <SCRIPT language=JavaScript>
     var img_width = "468";
    var img_height = "60";
    var img_title = "WHATEVER YOU WANT";
      var ad=new Array()
    //insert here your images src
    ad[0]='http://www.yoursite.com/images/image1.whatever';
    ad[1]='http://www.yoursite.com/images/image2.whatever';
    ad[2]='http://www.yoursite.com/images/image3.whatever';
    ad[3]='http://www.yoursite.com/images/image4.whatever';
    ad[4]=''http://www.yoursite.com/images/image5.whatever';
    ad[5]=''http://www.yoursite.com/images/image6.whatever';
    ad[6]='http://www.yoursite.com/images/image7.whatever';
     var links=new Array()
    //insert here your links
    links[0]='http://www.linksite1.com';
    links[1]='http://www.linksite2.com';
    links[2]='http://www.linksite3.com';
    links[3]='http://www.linksite4.com';
    links[4]='http://www.linksite5.com';
    links[5]='http://www.linksite6.com';
    links[6]='http://www.linksite7.com';
     var xy=Math.floor(Math.random()*ad.length);
     var xy=Math.floor(Math.random()*ad.length);
    document.write('<a href="'+links[xy]+'" target="_blank"><img src="'+ad[xy]+'" width="'+img_width+'" height="'+img_height+'" alt="'+img_title+'" border="0"></a>');
    </SCRIPT>
    
    (WOW - I can't beleive I answered a coding question!!!)
     
    • Thanks Thanks x 1
  7. OTrap

    OTrap Jr. VIP Jr. VIP

    Joined:
    Jul 12, 2008
    Messages:
    2,282
    Likes Received:
    1,051
    PERFECT, Autumn! JUST what I was looking for! Thanks and reps to all of you (unless I repped Greywolf too recently)!

    Grey, how's the plan coming?
     
  8. DisavowedKing

    DisavowedKing Regular Member

    Joined:
    Sep 1, 2010
    Messages:
    243
    Likes Received:
    84
    Gender:
    Male
    Occupation:
    Marketing - Email, Direct Mail, and Internet
    Location:
    San Diego, CA
    Does anyone have a php based rotating ad script that has an admin area so you can track the amount of impressions and clicks on an ad?
     
  9. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    http://www.openx.org/ is quite a popular one, it's not exactly lightweight though.