PHP in CSS: Dynamic Background Color... Ack!!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

PHP in CSS: Dynamic Background Color... Ack!!!!!

Post by mhulse »

Title: PHP in CSS: Dynamic Background Color... Ack!!!!!

Hola folks! I will try to be as clear as possible... if I am not, please ask me to clarify. Ok, here it goes:

Here is my CSS PHP code:

Code: Select all

<?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:

Code: Select all

<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):

Code: Select all

<?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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

echo $page var before your if statement ;)
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

Hmmm, like this:

Code: Select all

<?php
// Let the browser know this is a CSS file.
header('Content-type: text/css');

// Define some variables:
$bkgrnd = "#333"

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" => "$bkgrnd", "About" => "#0066cc", "Folio" => "#0066cc", "Resume" => "#0066cc", "Code" => "#0066cc", "Forum" => "#0066cc", "Contact" => "#0066cc", "Links" => "#0066cc");
	$my_colors = $page_colors[$link];

	$page = str_replace("/", "", $_SERVER['SCRIPT_NAME']); 
	echo $page;
	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"); ?>;
}
It did not seem to change anything... Thanks for you help though... I appreciate the quick response :D

Any other suggestions?
Thanks
Cheers
micky...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

look at the page source.... it would always be something like 'optional_css.php'. Obviously it's never equal to any element from $pages array, so $pages[$link]==$page always evaluates to false.
Post Reply