Support Forums
Change Background Color using php - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: Change Background Color using php (/showthread.php?tid=1379)

Pages: 1 2


Change Background Color using php - immi - 10-13-2009

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


RE: Change Background Color using php - El Zorro - 10-13-2009

how does it now what day it is?


RE: Change Background Color using php - Gaijin - 10-13-2009

(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);?>"



RE: Change Background Color using php - R3c0nna1ssanc3 - 10-13-2009

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


RE: Change Background Color using php - immi - 10-14-2009

(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


RE: Change Background Color using php - Gaijin - 10-14-2009

(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


RE: Change Background Color using php - immi - 10-14-2009

(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 ;)


RE: Change Background Color using php - Gaijin - 10-14-2009

(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().'">'



RE: Change Background Color using php - immi - 10-14-2009

(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


RE: Change Background Color using php - Gaijin - 10-14-2009

(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