Forum Moderators: coopster

Message Too Old, No Replies

Variable Creation - NEW TO PHP

Dynamically creating color schemes on different pages.

         

abacus566

4:46 am on Jul 15, 2007 (gmt 0)

10+ Year Member



Okay, let me start by saying I'm new to PHP.
I am creating a website with 5 subsections, each section having a unique color that the design scheme is based off. I've created a colors.php:

$section1="FF0000";
$section2="00FF00";
$section3="0000FF";
$section4="FFFF00";
$section5="00FFFF";

Each section is under a different subfolder in the site root:

www/section1/
www/section2/
www/section3/
www/section4/
www/section5/

What I want is for a page in any section's subfolder to dynamically be colored. So if I put page test.php in section1 subfolder it would be colored #FF0000 (red) but if I moved it to section4 subfolder it would be colored #FFFF00 (yellow).

I think what I need is PHP_SELF but am not sure how to strip that down to reflect only the subfolder. Then once I have that subfolder, I'm not really sure how use it to pull from my included file colors.php.

If I can be any clearer--please let me know. I thank you in advance for your help and patience with a PHP novice!

Habtom

5:04 am on Jul 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



color_config.php
<?php
function back_color($url) {
$folder_w = explode("/",$url);
$folder_c = $folder_w['3'];
if ($folder_c == "section1"){
$color = "FF00000";
}
return $color;
}
?>

All Pages
<?php include("color_config.php");?>
<body bgcolor="<?php back_color($url);?>">

abacus566

7:11 pm on Jul 15, 2007 (gmt 0)

10+ Year Member



Thanks Habtom!

It took a little configuring, but that code worked like a charm.
Here's what I ended up doing:

In file color_config.php:

function back_color($url) {
$filename = $_SERVER['PHP_SELF'];
$folder = substr($filename, 0, strrpos($filename, '/'));
if ($folder == "/subfolder"){
$color = "#FF0000";
}

...and so on--with an IF statement for each section.

Within each of my actual webpages I placed:

<?php include "../includes/color_config.php"?>

and wherever I wanted the color to print, something like this:
bgcolor="<?php print back_color($url);?>"

Thought I'd include that in case anyone would be interested?