3

Trying to get a random background between two images.

The PHP (Before Doctype in index.php):

<?php
$bg = array('back1.jpg', 'back2.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

CSS:

body {
  height: 100%;
  margin: 0;
  color: #474747;
  font: 13px/23px  'Exo 2', sans-serif;
  min-width: 1186px;
  background: url( ../img/<?php echo $selectedBg; ?>) no-repeat 50% 50%;
  background-size: cover;
  background-attachment: fixed;
}

The issue is that the background isn't set to either of the images!

EDIT:

Ive moved on to a completley PHP method although it errors:

Line : 5,   Error type : 4
Message : syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$'

Code:

<?php
  $bg = array('back1.jpg', 'back2.jpg' ); // array of filenames
  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = "$bg[$i]"; // set variable equal to which random filename was   chosen
  $('body').css( {                        'background-image': 'url( img / ' +     echo $selectedBg + ')'            } );
?>
5
  • this $('body').css( {... is JS/jQuery and doesn't belong in your PHP and isn't PHP code, that's why you're getting that error. Commented Dec 24, 2015 at 1:00
  • I know, Ive moved it into my script side of things and it still wont set it Commented Dec 24, 2015 at 1:05
  • I don't know what you're telling me here. I mentioned that you have Javascript/jQuery code inside PHP and it doesn't work like that; hence, the syntax error. You need to seperate your JS from your PHP and make sure the jQuery library is loaded. E.G. api.jquery.com/html Commented Dec 24, 2015 at 1:07
  • Yes, Ive separated the code. Although it still doesn't want to work. Commented Dec 24, 2015 at 1:11
  • your question's code says otherwise. You need to edit your question with the exact code you're using and in what way. Commented Dec 24, 2015 at 1:14

2 Answers 2

1

What you're looking for would be

array_rand();

PHP Manual: http://php.net/manual/en/function.array-rand.php

for your PHP:

<?php
$bg = array('back1.jpg', 'back2.jpg' ); // array of filenames
$i = array_rand($bg); // generate random number size of the array
$selectedBg = $bg[$i]; // set variable equal to which random filename was chosen
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Aha, I should clarify! The name oft he images is stored its just the background isn't set to the image.
The cleanest way to do this would be separate classes for each background and then just randomise the class with PHP instead of physically modifying the CSS with it.
@jacob Explain please
0

Your css file should have something like this in it

.one {
background: url(../img/back1.jpg);
}

.two {
background: url(../img/back2.jpg);
}

.three {
background: url(../img/back3.jpg);
}

And your HTML should have

<body class="<? echo $img ?>">

And then your PHP need only define $img as either one two or three

<?
$list = array('one', 'two', 'three' );
$i = array_rand($list);
$img = $list[$i];
?>

1 Comment

You would expose all your image files on the web isn't it? with PHP array_rand() you still keep some confidential. Please correct me if I'm wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.