In a recent HTML project, I'm trying to make a 's background-image change everytime the page is loaded, using PHP. I know both CSS and HTML, but not PHP.
As I'm a newbie with PHP, I decided to search how to select a random image inside a folder using PHP and I used the solution to this question from StackOverflow.
The PHP code seems to work, as the page doesn't throw any errors. The image is not being displayed as the background of the div (not image is being displayed).
Here's the piece of code:
<?php
$directory = "/images/headerBg/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
$IMG = mt_rand(1, $filecount);
?>
<style type="text/css">
.bgImg {
background-image: url("/images/headerBg/<?php echo $IMG ?>.jpg");
}
</style>
<div class="bgImg">
<!-- The div with the changing background -->
</div>
Note 1: Files are named 0.jpg to the last one (n.jpg).
Note 2: I can't use arrays, as I'll add more photos on the way.