Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brianellisrules

macrumors regular
Original poster
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:
PHP:
<img src="http://www.brianellisrules.com/background<?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.

BUT!

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?

Thanks!
 
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:

PHP:
<? $num = rand(1,5); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>phpstyle.php</title>
	<meta name="generator" content="BBEdit 7.1.1">
	<link rel="stylesheet" href="allstyles.css" type="text/css">
	<style type="text/css" media="all">
	<!--
		.test
		{
			color: #003C73;
			font-family: Verdana;
			font-size: <?=$num?>em;
		}
	
	-->
	</style>
	
</head>
<body>
<div class="test">Woo Hoo!</div>
</body>
</html>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.