Jump to content

Random fixed background


usawh

Recommended Posts

  Quote

Hi,

 

How to I edit this script so that everytime a user go to my site they get a ramdom fixed background. Here is the script..

 

<style type="text/css">

  body {

    background-attachment:fixed;

    background-image:url(background_image.jpg);

  }

</style>

 

regards,

usawh

 

Assuming this involves PHP?

 

$bg_arr = array("background1.jpg","something.gif","another.png");
shuffle($bg_arr);
?>
<style type="text/css">
  body {
    background-attachment:fixed;
    background-image:url('<?php=$bg_arr[0] ?>);
  }
</style>

 

Just a quick, way... shuffle($arr) shuffles the array so when you call it again, it's shuffled.

Or use proper syntax (and I prefer array_rand()):

 

<?php
$bg_arr = array('background1.jpg', 'something.gif', 'another.png');
echo '<style type="text/css">
  body {
    background-attachment:fixed;
    background-image:url("' . $bg_arr[array_rand($bg_arr)] . '");
  }
</style>';
?>

 

If people would just stop using short tags and echo shortcuts, this post wouldn't be necessary.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.