Dynamic Background with PHP and CSS

Dynamic Background with PHP and CSS

sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
So you know those retarded pages where the background is static and everything else moves over it? Well I'm making one of those. My problem is I want the background to change every so often (refresh, hour, day, whatever). How would I do this?
 
 
 
2010 Nov 3 at 17:23 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
php code

<?php

$backgrounds
= array('cat.jpg','dog.jpg','elephant.jpg');

$hour = date('G');

$imgnum = $hour % count($backgrounds);

$img = $backgrounds[$imgnum];

echo
"<body style='background-image:url(images/$img);'>";

?>

 
 
 
2010 Nov 3 at 22:57 PDT — Ed. 2010 Nov 3 at 22:57 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
I tried something similar to that, but it doesn't any background...
php code

<?php
$backgroundNum
= rand(0,10);
switch (
$backgroundNum)
{
case
0:
{
$background = "backgrounds/warlone.jpg";
break;
}
case
1:
{
$background = "backgrounds/cs.jpg";
break;
}
case
2:
{
$background = "backgrounds/ak.jpg";
break;
}
case
3:
{
$background = "backgrounds/art.jpg";
break;
}
case
4:
{
$background = "backgrounds/csjump.jpg";
break;
}
case
5:
{
$background = "backgrounds/guyDestruction.jpg";
break;
}
case
6:
{
$background = "backgrounds/guyFire.jpg";
break;
}
case
7:
{
$background = "backgrounds/hl2Gordon.jpg";
break;
}
case
8:
{
$background = "backgrounds/hl2Guard.jpg";
break;
}
case
9:
{
$background = "backgrounds/timeSquare.jpg";
break;
}
case
10:
{
$background = "backgrounds/warRight.jpg";
break;
}
}
?>
<html>
<head>
<style type="text/css">
#bg
{
background: url(<?php echo "$background"; ?>) no-repeat fixed center top;
width: 100%;
display: table;
}
</style>
</head>
</html>

 
 
 
2010 Nov 4 at 10:34 PDT — Ed. 2010 Nov 4 at 10:35 PDT
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Isn't switch used for when you want the user to give input? Like, they'd have to press a number to pick the background in your case... I think.
 
 
 
2010 Nov 4 at 10:39 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
No, switch is basically a bunch of if statements.

I jus' noticed something:
code
background: url(<?php echo "$background"; ?>) no-repeat fixed center top;


The echo "$background"; may jus be echoing background, and not the value. My web server is down right now so I can't test out this theory. I know print "$background"; would output the value of background, but I'm not sure about echo.
 
 
 
2010 Nov 4 at 10:41 PDT — Ed. 2010 Nov 4 at 10:44 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
echo, print, it doesn't matter. The value of $background will be used because it's a "double-quoted" string. By the time echo or print "see's" it, the variable has already been resolved.

Also you can run PHP on any computer. You don't need a server to test/build stuff.
 
 
 
2010 Nov 10 at 14:58 PST
SuperJer
Websiteman

2005 Mar 20 • 6629
Or you can run PHP right in your browser at: ideone.com

And other languages, too!

Don't forget the <?php at the beginning.
 
 
 
2010 Nov 10 at 15:02 PST
VMAN

2009 Feb 15 • 8
PHP code
background: url(<?php echo "'" . $background . "'"; ?>) no-repeat fixed center top;
-VMAN
 
 
 
2010 Nov 15 at 19:28 PST
Page [1]