Jump to content

Recommended Posts

hi

 

im trying to create a script that swaps image folders depending on the season. This can be used for website headers or adverts so during easter for instance you could swap to a theme for your header image without manually doing it. when the folder is chossen the script randomly chooses an image form the folder.

 

Its not working however hense this post, anyone with even a small amount of php knowlege will probably spot the problem straight away.

Please could you help me get this working.

I call it using a css style

 

background: #0c1a3e URL(../rotate.php) no-repeat center center ;

 

Thanks

 

 

 

<?php
// 23march to20june  spring

if ((date('m') == 03) && (date('d') >= 23) || (date('m') == 06) && (date('d') <= 20)) { $folder = './images/personal/headers/spring/'; }


// 21june 22sept  summer
else if ((date('m') == 06) && (date('d') >= 21) || (date('m') == 09) && (date('d') <= 22)) { $folder = './images/personal/headers/summer/'; }

// 23 sept to dec 20 autumn

else if ((date('m') == 09) && (date('d') >= 23) || (date('m') == 12) && (date('d') <= 20)) { $folder = './images/personal/headers/autumn/'; }


// 21 dec to 22 march winter

else ((date('m') == 12) && (date('d') >= 21) || (date('m') == 03) && (date('d') <= 22)) { $folder = './images/personal/headers/winter/'; }

//otherwise

// else { $folder = './images/personal/headers/'; }

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

Edited by vote-4-pedro
Link to comment
https://forums.phpfreaks.com/topic/271406-script-to-swap-images-based-on-date/
Share on other sites

That's a lot of work for every single page load. However,

 

2) Type the address of the rotate.php script into your browser's address field. If it works, you should see the image. If it fails, you might see some error messages.

 

1) Turn on error reporting: error_reporting(-1); ini_set('display_errors', 1); at the beginning of the script so you can see any errors that are occurring. Fix them all (including warnings and notices!)

 

3) Check the relative path names you are specifying. For opendir() it is looking relative to the path where the main script that is running right now is located (i.e. rotate.php). For the Location: header, well, the standard says you are supposed to supply a full url -- with the http:// and the domain and the full (web) path to the file. Since you are specifying a relative path, the browser is free to determine what it is relative to when making its request.

 

4) In the CSS file, you have specified a relative page. I think that is relative to the script that contains the LINK tag that loads the CSS file. A full url would probably be better here as well.

 

#1) When you tell us "its not working" we have absolutely no idea what you mean. Tell us what it does that it should not do, or what it does not do that it should do. Look in the View->Source of your browser and see if there is any non-rendered stuff there.

thanks for your help

 

working now and improved . how would this be for work per page? a lot thats what! would jscript be a better method?

 

<?php
//get a timestamp for the start of each season
$spring = strtotime('23 march');
$summer = strtotime('21 june');
$autumn = strtotime('23 september');
$winter = strtotime('21 december');
//get the current timestamp
$current = time();
//then check each season in reverse order
//if past winter start
if($current >= $winter){
$season = "winter";
//if past autumn start
} elseif($current >= $autumn){
$season = "autumn";
//if past summer start
} elseif($current >= $summer){
$season = "summer";
//if past spring start
} elseif($current >= $spring){
$season = "spring";
//if before spring start, must be previous year winter still
} else {
$season = "winter";
}
//get the full folder using the season var set above
$folder = "./images/personal/headers/{$season}/";
//get an array of files matching the extensions jpg,jpeg,png,gif in that folder
$files = glob("{$folder}*.{jpg,jpeg,png,gif}", GLOB_BRACE);
//get a random key from the array
$rand = array_rand($files);
//get the filename from the list of files
$filename = $files[$rand];
//get the extension to determine header
$ext = substr($filename, strrpos($filename,'.'));
//send out correct header based on ext
header("Content-Type: image/{$ext}");
//output the file
readfile($filename);
header('Location: '.$filename); // Voila!
?>

Edited by vote-4-pedro
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.