Support Forums

Full Version: Change Background Color using php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
how does it now what day it is?
(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);?>"
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
(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
(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
(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 ;)
(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().'">'
(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
(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
Pages: 1 2