Title: PHP in CSS: Dynamic Background Color... Ack!!!!!
Hola folks!
Here is my CSS PHP code:
<?php
//file name: optional_css.php
('Content-type: text/css');
function css_color($link)
{
$pages = array ("Home" => "home.php", "About" => "about.php", "Folio" => "folio.php", "Resume" => "resume.php", "Code" => "code.php", "Forum" => "forum.php", "Contact" => "contact.php", "Links" => "links.php");
$page_colors = array ("Home" => "#0066cc", "About" => "#0066ff", "Folio" => "#009988", "Resume" => "#6666cc", "Code" => "#006622", "Forum" => "#cccccc", "Contact" => "#009999", "Links" => "#000000");
$my_colors = $page_colors[$link];
$page = str_replace("/", "", $_SERVER['SCRIPT_NAME']);
if($page == $pages[$link]) {
echo "$my_colors";
} else {
;
}
}
?>
body {
background-color: <? css_color("Home"); css_color("About"); css_color("Folio"); css_color("Resume"); css_color("Code"); css_color("Forum"); css_color("Contact"); css_color("Links"); ?>;
}
I use this line of code to import the above CSS in my header include:
<style type=\"text/css\" media=\"screen\"> @import \"css/optional_css.php\"; </style>
I want to use one CSS file to dynamically load-in a different CSS background color depending on the page that is loaded...
The above code works if I change it to this (but is not (as) dynamic):
<?php
header('Content-type: text/css');
$color1 = "#FF0000";
?>
body {
background-color: <? echo $color1; ?>;
}
Sure, I could code it so each page loads in a diff CSS file for each page of my site, but I would like to just use one CSS file to set the body background style of each main page/section...
When it comes to the first block of code (one with function), am I doing things right? I know that the basic syntax of my function "function css_color($link)" works for other dynamic parts of my code (Current Menu Linkage, Dynamic Loading Header Content...)
The only suggestion I have recieved so far (from other forum) is this:
The css file is loaded into cache. How you would do this is to define seperate classes for different colors, then reference the class you want in your dynamic php code.
Can someone elaborate on the above quote for me? I think I get it, but I am having troubles writing the algorithm.
Any suggestion?
Any help would be greatly appreciated.
Thanks in advance.
Cheers
Micky