Log in

View Full Version : how can we include php file within css file



manisha007
12-05-2006, 11:14 AM
Hi everyone,

I am working a color varying site now. I want to change the color for every page using CSS.

Since, I hav a colors on my database. I jus want to call the color in the css file.

I havn't idea that how to declare a php variable(e.g.., $color) in the style.css file.

Pls, suggest me

Thanks,
Manisha

djr33
12-05-2006, 11:38 AM
You can't.
...kinda.

The solution is to output a css stylesheet with php.
Basically...
css.php:
<?php
echo "a {color:blue;}";
?>

I don't think that css needs any particular format notes, like a header, but php can also output those if needed.


Additionally, you could add .css as a php-interpreted extension... how to do so is around here somewhere... then any <?php ... ?> tags would be interpreted in .css files just like .php.

ItsMeOnly
12-05-2006, 05:01 PM
That needs to be a little trickier- since Moz/FF is picky about proper MIME type- and PHP will by default return text/html. Therefore you'll need to send
header('Content-type: text/css')
to make it work right.

After that: you'll need to assign a handler or type to css files themselves
like in apache

AddType application/x-httpd-php .css

Twey
12-05-2006, 06:15 PM
After that: you'll need to assign a handler or type to css files themselvesThis is inefficient, since all CSS will be parsed as PHP. Better to simply give it a .php extension:
<link rel="stylesheet" type="text/css" href="style.css.php">

windowpane
12-05-2006, 07:25 PM
Hi
You can iclude a php file into <head> section of your pages like that;
<style>
<?php include ("style.php"); ?>
</style>

style.php behaves like a css file now.

Define variable $color;
and you can use it in the style.php like a property of your css-classes like;
body {
color: <?echo $color;?>
}

In the code below, $color is changed according to page name, via variable $page. But this solution isn't useful, you must define every pages in if-else statement at the style.php.

Content of style.php

<?
$page=$_SERVER["SCRIPT_NAME"];
$color;

if ((strpos($page, "default.php")>0)){
$color="#000000";
}
else if ((strpos($page, "a.php")>0)){
$color="#E2E2E2";
}
else if ((strpos($page, "b.php")>0)){
$color="red";
}
?>

body {
font-family:arial;
color:<?echo $color;?>
}