php and background images

Discussion in 'Web Design and Development (archive)' started by brianellisrules, Jan 15, 2004.

  1. brianellisrules macrumors regular

    Oct 17, 2003
    OK, based on recommendations from my other thread (this one) I'm going to be adding a background image to my webpage. Piece of cake.

    Here's the deal, I want to have it change the image randomly. No problem, if it was a standard html/php, I'd just do something like:
    <img src="<?php rand(1,3);?>.jpg">
    and let that go to town. I'm not 100% sure on the syntax, but basically that'd add a random number to the file name and then I'd have my random background, right? (assuming I have files named background1.jpg, background2.jpg, background3.jpg, etc) I think so.


    That means I'd have to go to every single file and add that line to it. Not cool. With CSS you can handle that in one line, but I don't think you can put the php in there... can you? Is there a way around this? Help? Anyone? Beuller?

  2. arson macrumors member

    Jan 12, 2004
    Re: php and background images

    Do you have a separate file for your header? If so, you can dom something like defining your random variable at the top, and including just one style in the header, leaving the rest in your separate CSS file. Here is an example that changes the size of the text:

    <? $num rand(1,5); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        <meta name="generator" content="BBEdit 7.1.1">
        <link rel="stylesheet" href="allstyles.css" type="text/css">
        <style type="text/css" media="all">
                color: #003C73;
                font-family: Verdana;
                font-size: <?=$num?>em;
    <div class="test">Woo Hoo!</div>
  3. brianellisrules thread starter macrumors regular

    Oct 17, 2003
    Yes I do....

    I just tested it out, and it worked! Genius!


Share This Page