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 + ')' } );
?>
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];
?>
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
?>