Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Randomised seasonal images with PHP

Featured Replies

I did a site for a client years ago and the header images are all in one folder and are randomised using this attached random.php file:

 

rotate.php

 

All pretty simple...but...The client wants the images to change depending on the saeson so, as the site was built ages ago when I was less savvy about dynamic content, I go into the site every season and switch the images in the folder to be the Spring, Summer, Autumn or Winter ones. It's a bit of a pain and even though I get paid to do this by the client I think it would be better if this was now made automatic.

 

I wondered if I could use something like this:

<img src="image/<? echo(date('F'))?>.jpg" />

 

and adapt it somehow so that instead of picking out images named by month it would picks out images named by saeson and also randomise them.

 

Can anyone help as I'm a total novice when it comes to PHP.

 

Thanks

 

E

  • Author

or maybe something could be added to the rotate.php file to make it do the randomisation according to season?

Nice solution Rallport, would do the trick :)

 

+ 1

  • Author

hi rallport - thanks for that - does that involve randomisation or the display of just one image?

I need the images to randomise and there are 10 images for each season.

  • Author

Wow - super fast response - I'll go and try this out now.

 

Thanks

 

E

  • Author

stumbling already - do I just put all this within opening and closing php tags?

 

also how do I finish this off after I've listed all four seasons:

 

} else ...etc.

 

and where does this go:

$yourRandomImage= $myImages[array_rand($myImages)];

echo $image22;

 

and why does it have echo $image22; at the end?

// function to return season based on month
function getseason($month) {
switch($month) {
	case 12:
	case 1:
	case 2:
	$s = 'winter';
	break;
	case 3:
	case 4:
	case 5:
	$s = 'spring';
	break;
	case 6:
	case 7:
	case 8:
	$s = 'summer';
	break;
	case 9:
	case 10:
	case 11:
	$s = 'autumn';
	break;
}
return $s;
}

// Array of seasons and their image count
$seasons = array(
'winter' => '10',
'spring' => '10',
'summer' => '10',
'autumn' => '10'
);

$season = getseason(date('m'));
$image = '<img src="images/'.$season.mt_rand(1, $seasons[$season]).'.jpg" alt="'.$season.'" />';
echo $image;

The numbers in the seasons array are the maximum number for that season. Say winter has only 5 images, change the 10 to 5.

Your images need to then be named winter1.jpg, winter2.jpg, winter3.jpg, winter4.jpg, and winter5.jpg. Note that the numbers start at 1 not 0

  • Author

will digest this later - got to take the dog out in the howling wind / rain / sleat / snow - I may be some time...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.