Forum Moderators: coopster

Message Too Old, No Replies

Creating a Dynamic CSS file using PHP

Is it possible?

         

cosmoyoda

6:34 pm on Jan 1, 2008 (gmt 0)

10+ Year Member



Hi everyone,

Today I asked myself a simple question that could save me a lot of time coding CSS styles. However, this question might be a little dumb. I'm not familiar with all the powerful capabilities of PHP, so I figured out I should just ask here.

Is it possible to create a dynamic CSS file using PHP, for instance this:

themes.css.php

- This .css file would have many many styles, but instead of placing "color: (some color here)" all over it, I would just have something like "color: <?php echo $rsThemes['ThemeColor'];?>". That means I can just pull out of the database any theme color I want for this particular CSS file and have that color changed in seconds.

- The final output with the query strings would look something like themes.css.php?ThemeColor=black. What this would do have all the colors set as black.

So, looking at my example could you guys tell me if it is possible or not to have this "dynamic CSS file" or is this just in my imagination?

Thank you for your help and have a great new year.

PHP_Chimp

6:56 pm on Jan 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you are looking at is quite possible.
I would suggest that you look at a few tutorials about passing values using $_GET. There are loads on the web if you google for them.

As then you can call your css script using

themes.css.php?ThemeColor=black

You would use statements like -


color:$_GET['ThemeColor'];
// or if you dont know what you may be setting
if ($_GET['ThemeColor']) {
color:$_GET['ThemeColor'];
}
else { // set a default color
color:fff;
}

You will also need to make sure that you send a


header('Content-Type: text/css; charset: UTF-8');

As otherwise php output wont be read as css.

cosmoyoda

7:15 pm on Jan 1, 2008 (gmt 0)

10+ Year Member



That makes a lot of sense to me now. I'll take a look at a few tutorials and read about this. It seems quite easy.

Thank you so much!

jatar_k

7:17 pm on Jan 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Server Side Scripting in CSS Files [webmasterworld.com]