PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Old November 30th, 2012, 08:10 AM
vote4pedro vote4pedro is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 vote4pedro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
php4 - Script that swaps image folders depending on the season

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 Code:
<?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!
?>

Reply With Quote
Old November 30th, 2012, 12:04 PM
IAmALlama IAmALlama is offline
Me
Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,290 IAmALlama User rank is Lance Corporal (50 - 100 Reputation Level)IAmALlama User rank is Lance Corporal (50 - 100 Reputation Level)IAmALlama User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 11 h 27 m 17 sec
Reputation Power: 10
First, your date matching is incorrect. You will have better luck using timestamps and time()/strtotime().

You can do something like:

PHP Code:
//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";



As for the reading a random file, what you have might work (I didn't test it, it looks ok) but there are better functions that are easier to work with such as glob().

PHP Code:
//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($filenamestrrpos($filename,'.'));

//send out correct header based on ext
header("Content-Type: image/{$ext}");

//output the file
readfile($filename); 


I also changed the header redirect that you were doing with what I feel is better which is to just output the header and read the file.
Comments on this post
vote4pedro agrees!

Reply With Quote
Old December 2nd, 2012, 04:53 PM
vote4pedro vote4pedro is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 vote4pedro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 36 sec
Reputation Power: 0
Awesome Thanks

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > php4 - Script that swaps image folders depending on the season


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap