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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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   1728 Views


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?


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>


Community Member, 79 Posts

14 November 2008 at 10:15pm

Many Thanks for your help.
All working now.