Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Random Background image


Go to End


3 Posts   1921 Views

Avatar
mhull

Community Member, 79 Posts

14 November 2008 at 10:37am

I have this code making a random image:

public function RandomImage( $strLeftDirName ) 
   { 
      if ( !is_string( $strLeftDirName ) ) 
      { 
         return false; 
      } 
      $left_dir = DataObject::get_one("Folder", "Name = \"$strLeftDirName\""); 
      $randImg = ( $left_dir->ID ) ? DataObject::get_one("Image", "ParentID = $left_dir->ID", true, "RAND()" ) : false; 
      return $randImg->Filename; 
   }

But how would I make it a random background image on a div?
I want to be able to apply it to my header if possible?

Avatar
Sean

Forum Moderator, 922 Posts

14 November 2008 at 2:56pm

Edited: 14/11/2008 2:57pm

Apply it via inline CSS on the element. e.g.:

<div id="header" style="background-image: url($randomHeaderImage)">
<h1>My website</h1>
</div>

Avatar
mhull

Community Member, 79 Posts

14 November 2008 at 10:15pm

Many Thanks for your help.
All working now.