Database driven affiliate websites

Random Images

I said I would do some work on the foundation of this weblog.

See the sidebar over there, the one with my lovely mugshot? Yeah, that one. I hardcoded some of the info in there, in particular the section that includes my picture.

If you were to hit the refresh button, another picture would show up. Magic, huh?

Not really, but it did use a little PHP to get that impact.

Here's what I did.

1) Login to the admin section.

2) Click on Presentation.

3) Click on Theme Editor, then for this theme, I clicked on sidebar.php.

Here's where the PHP magic occurs.

randomize the images

That image might be a little tough to see, here's the important part.


<img src="http://www.davewooding.com/images/<? echo rand(1,6); ?>.jpg">


Note the php echo statement in there. Before I explain, I should tell you that I created 6 images to use for this and labeled them 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg and ... drumroll, please ... 6.jpg.

That php echo statement in there with the rand function says to pick a random number (1 thorugh 6) and print that out. Notice that it prints out in the middle of an image source statement - that's how I get a random picture to show up.


Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Sphinn
  • Mixx
  • StumbleUpon
  • MisterWong

4 Responses to “Random Images

  • 1
    Brian
    March 2nd, 2008 19:02

    Good tip.

    How do you do the same for random snippets, or lines of text that you have in one txt file?

  • 2
    Random Text
    March 3rd, 2008 13:27

    [...] Brian asked the following question in a reply to the random image post I made. How do you do the same for random snippets, or lines of text that you have in one txt file? [...]

  • 3
    Romantic Bedroom Ideas
    March 6th, 2008 16:14

    I've seen this done with random header graphic images... but never saw the point.
    I realize that when some one is clicking through your site another image will show up, but I think this would create confusion.

    Could you post some ideas that this technique might be useful for? My thinking outside the box cap is lost. :(

    Debra THAT Painter Lady.com

  • 4
    admin
    March 6th, 2008 22:48

    @Debra,

    For this site, the only reason I am doing the random image display is to keep things interesting - pretty much just for entertainment purposes.

    You want outside the box, OK.

    One thing you could do is use it as a banner ad display system. Say you have five advertisement images you want to display. Using the random image selection, you could display a random advertisement on each page load. Add some more PHP functionality and you could also track which images get the most clicks.

    Another idea not exactly related, but something that I actually did. I would rotate different variations of Google Adsense across a portfolio of web sites on different days of the month. All of the odd days, I would show one version and all of the even days, I would show another version. This way, I had a very simple tracking system when I logged into my account.

    The code looked something like this:

    < ?php

    $day = date("j");
    $day = $day % 2; // <- there's that modulus thing I keep talking about
    if ($day < 1) {
    print "Google Adsense 1";
    } else {
    print "Google Adsense 2";
    }

    ?>

    Dave

Leave a Reply