PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
[back]
12/20/99 "Using Include To Avoid Unnecessary
Repetition"
[next]
Did you notice that now we are actually in a PHP-enabled page and not in an HTML page any longer? The dynamic background script from yesterday's diary is in use here, so depending on the day of the week that you are viewing this diary entry page, the background color will change.
Since I'd like to use this script on every diary page, I don't really want to type (well cut and paste anyway) the same 8 lines of code repeatedly. I can make 7 of the 8 lines of code a separate file by using the function include and the change background based on day script will be on every page. Now if I want to go back later and change or add, I can change all my diary pages at one time, instead of having to go into each diary page to change the code individually. Use this technique rather than repeating code unnecessarily. Now if I did want to have one different page, I simply would code that page rather than using the include body_change.php3 file. Here is the code to call the file using the include function:
<? include("body_change.php3"); ?>
Now here's the code actually inside the body_change.php3 file:
<?
$today = date("w");
$bgcolor = array(
"#FEF0C5",
"#FFFFFF", "#FBFFC4", "#FFE0DD",
"#E6EDFF",
"#E9FFE6", "#F0F4F1"
);
?>
Now the only other line I only need to add, is replacing my HTML <body bgcolor=""> tag to read:
<body bgcolor="<?print("$bgcolor[$today]");?>">
I am going to be tinkering around with the body tag and body_change more later. But you can already see how easy it could be changed using the included body_change file to a number of different things. I could add random text colors or background images, just to name a few and I could change them all using the single body_change file. We'll definitely be playing with this again later.
[back]
12/20/99 "Using Include To Avoid Unnecessary
Repetition"
[next]
PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
Copyright 2000 php-scripts.com