Background Image rotator

Ask about a PHP problem here.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Background Image rotator

Post by Fidbeck »

What's the best way to create an automatic image background rotator?
I mean from January 1st to 21st of March have a winter related image background and from 22nd of March to 21st of august haver spring image and so on.
for the specific dates is better to use a case statement of an if statement?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Background Image rotator

Post by jacek »

I would names the images based on their date like "background_01-01.jpg" then you can use date()

[syntax=php]<img src="background_<?php echo date('d-m'); ?>.jpg" alt="bg" />[/syntax]
That way it will be easy to change one of the images.
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Background Image rotator

Post by Fidbeck »

Care to elaborate?
And if I want to change to different pics in specific days like Christmas, New Year, Easter, Halloween and so on xD
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Background Image rotator

Post by jacek »

Oh I think I misread, The above would give you a background unique to each day. If you only want a few variations for random lengths of time it probably is best to use a if else if else

Something like this

[syntax=php]$date = date('d-m');
$month = date('m');

if ($date == '25-12'){
// xmas one
}else if ($date == '26-09'){
// Jacek's birthday
}else if ($month < 3){
// winter
}else if ($month < 6){
// spring
}else{
// default
}[/syntax]
add as many as you need.

You could also use the date comparison functions to be more accurate.
Image
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Background Image rotator

Post by ExtremeGaming »

jacek wrote:[syntax=php]
}else if ($date == '26-09'){
// Jacek's birthday
}[/syntax]


:lol:
<?php while(!$succeed = try()); ?>
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Background Image rotator

Post by Fidbeck »

Since this is for the Nucleus of Students of my faculty I have to add 2 dates, faculty and university's aniversaries.
The code is already fully functioning?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Background Image rotator

Post by jacek »

Fidbeck wrote:The code is already fully functioning?

Not sure what you mean by that :? The method above should work.
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Background Image rotator

Post by Fidbeck »

What I meant is that Can I apply the code as it is?
I already have 3 functioning codes lol
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Background Image rotator

Post by jacek »

Fidbeck wrote:What I meant is that Can I apply the code as it is?
I already have 3 functioning codes lol

You would have to change the dates and links for your case but pretty much.

Sorry for the delay :(
Image
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Background Image rotator

Post by Fidbeck »

I haven't tried it but maybe I will get a fourth imagem rotator function :P
Post Reply