Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change Background Color using php
#1
Hello Pirate

Keep enjoy using great SF forum Smile

SCRIPT DESCRIPTION:
--------------------
This changes the colour of the background depending on the day of the week! Just place this after the (/head tag) and before the (body) tag (which you will have to take out)

With this code you can make your page more attractive, by having random colors.

CODE:
-----------
PHP Code:
<?
$today 
date("w");
$bgcolor = array(
"#FF0000""#00FF00""#0000FF""#FFFF00",
"#FF00FF""#00FFFF""#FFFFFF"
);
?>

And change the <body> tags with the following code:

PHP Code:
<body bgcolor="<? print ($bgcolor[$today] - 1);?>"
Thanks Ninja Geek for help ;)

---

That's it Smile Enjoy
Reply
#2
how does it now what day it is?
Reply
#3
(10-13-2009, 03:53 PM)El Zorro Wrote: how does it now what day it is?

The date() function used witht the parameter "w" returns the number of a day (1-7), that is then used to ouput right member of $bgcolor array

@OP

You may want to consider this line as false.
PHP Code:
<body bgcolor="<?print("$bgcolor[$today]");?>"

The first element of an array is always = 0, so your line will never use the first member "#FF0000"
The solution is
PHP Code:
<body bgcolor="<? print ($bgcolor[$today] - 1);?>"
Reply
#4
Thanks for this im really bad at php and I love to learn new things about it.

what is this part for?

$today = date("w");

edit: never mind I get it now
Reply
#5
(10-13-2009, 04:31 PM)NinjaGeek Wrote: @OP

You may want to consider this line as false.
PHP Code:
<body bgcolor="<?print("$bgcolor[$today]");?>"

The first element of an array is always = 0, so your line will never use the first member "#FF0000"
The solution is
PHP Code:
<body bgcolor="<? print ($bgcolor[$today] - 1);?>"

Yes you are right actually, I have edited the line in FP also Smile
Reply
#6
(10-14-2009, 03:14 AM)immi Wrote: Yes you are right actually, I have edited the line in FP also Smile

np mate .... Blackhat
I lkie that you use date("w") and then directly access the array, I've seen so many people doing if and switches to get this little script to work Roflmao
Reply
#7
(10-14-2009, 03:18 AM)NinjaGeek Wrote: I lkie that you use date("w") and then directly access the array, I've seen so many people doing if and switches to get this little script to work Roflmao

Yours welcome ;)
Reply
#8
(10-14-2009, 03:25 AM)immi Wrote: Yours welcome ;)

And I had an idea based on this script..... Changes the color based on the four seasons

PHP Code:
function fourSeasons() {
    
$month date("m");
    
$color "#ff0000";
    if(
$month >= && $month <= 5) {
        
$color "#59bd0f";
    }elseif(
$month >= && $month <= 8) {
        
$color "#fece60";
    }elseif(
$month >= && $month <= 11) {
        
$color "#7e5f29";
    }elseif(
$month == 12 || $month <= 2) {
        
$color "#8ed7e8";
    }
    
    return 
$color;
}

print 
'<body bgcolor="'.fourSeasons().'">'
Reply
#9
(10-14-2009, 03:51 AM)NinjaGeek Wrote: And I had an idea based on this script..... Changes the color based on the four seasons

PHP Code:
function fourSeasons() {
    
$month date("m");
    
$color "#ff0000";
    if(
$month >= && $month <= 5) {
        
$color "#59bd0f";
    }elseif(
$month >= && $month <= 8) {
        
$color "#fece60";
    }elseif(
$month >= && $month <= 11) {
        
$color "#7e5f29";
    }elseif(
$month == 12 || $month <= 2) {
        
$color "#8ed7e8";
    }
    
    return 
$color;
}

print 
'<body bgcolor="'.fourSeasons().'">'


Nice idea. Smile
Reply
#10
(10-14-2009, 06:18 AM)immi Wrote: Nice idea. Smile

Yeah, but now you'll get that ugly brown I've put up there.. look at it in the winter or spring... nice colors. Roflmao
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 956 07-27-2020, 11:26 PM
Last Post: tk-hassan
  Change warning level color with php [help] KingOfHunt3rs 0 1,110 12-04-2014, 08:45 AM
Last Post: KingOfHunt3rs
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,705 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 6 Guest(s)