This is the classic code used for randomly selecting a background image on reloading the page. While this was working when I tested it originally, since using Laravel framework with slim and twig ... I don't know what needs to change ? At the moment it is not showing anything just a plain white background... :/
<?php
$bg = array('http://www.imageurl1.jpg',
'http://www.imageurl2.jpg',
'http://www.imageurl3.jpg');
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{padding-top: 40px;padding-bottom: 40px;
background: url('<?php echo $selectedBg; ?>') no-repeat center center fixed;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover;}
</style>
</head>
<body>
...
</body>
</html>
I just copied and pasted your code into a new Laravel 5 project and it works. I used these picture links:
$bg = array('http://i2.cdn.turner.com/nba/nba/dam/assets/150805103851-damian-lillard-brooklyn-nets-v-portland-trail-blazers.home-t6.jpeg',
'http://i2.cdn.turner.com/nba/nba/dam/assets/150805092335-archie-goodwin-dallas-mavericks-v-phoenix-suns.home-t6.jpeg',
'http://i2.cdn.turner.com/nba/nba/dam/assets/150805123124-klay-thompson-with-family-trayce-thompson-080515.home-t3.jpg');
Are you using Laravel 5? Did you put your code into the "/resources/views/welcome.blade.php" file? Did you first see a "Laravel 5" welcome screen displayed on your browser after installing Laravel before your tried altering the code?
and here is the result:
When I click "refresh" in the browser, the image changes.
So follow these steps:
Try a fresh install of Laravel 5 by running "composer create-project laravel/laravel p2"
Navigate to localhost/p2 (or your setup's equivalent) and make sure you see the welcome screen that literally says "Laravel 5" in large letters in your browser. If you don't see this screen, the issue is something completely unrelated to your code.
Copy and paste your code into the "/resources/views/welcome" file as shown in the screenshot above, with working jpg links (like in my example).
Then navigate to localhost/p2 again, hit refresh, and see if you don't get the above result :) If the image files you are referencing are extremely large, it may take more than a few seconds for them to load.
Here is the problem with your code.
$selectedBg = "$bg[$i]";
Solution:
$selectedBg = $bg[$i];