...

View Full Version : how should I handle a dynamic .css file?



jjshell
01-02-2006, 10:47 PM
Hello,

here is the idea, I would like to have different styles available for a website. The different styles would be stored in a database. Here is the html line including the .css file:


<link rel="stylesheet" type="text/css" href="/css/styles.css" media="screen, projection" />

The style will be passed through the querystring (&style=). The css file should then contain php code. Problem, it can't.

How do you handle this? What would be the best approach according to you?

GJay
01-02-2006, 11:00 PM
Either change the link to a.php, or config the server to treat .css as a php file (the former is 'better').
i.e.
<link rel="stylesheet" type="text/css" href="/css/styles.php" media="screen, projection" />
Then something like:


echo '#body {color:#fff;font-size:12pt;}';

etc. (driven by your db or whatever);

jjshell
01-02-2006, 11:02 PM
What about MIME type? isn't the .css extension to be here for a purpose? If not, why not just name regular stylesheets with a .txt extension?

:)

marek_mar
01-02-2006, 11:10 PM
You can set the PHP to output whatever header you like.

jjshell
01-02-2006, 11:40 PM
You can set the PHP to output whatever header you like.
great post... what does that mean?

Velox Letum
01-03-2006, 12:05 AM
No need to be sarcastic.

What it means is PHP can masquerade as anything you want. CSS, images, XML, anything. A header is what tells the receiving party exactly what it is, so it knows what to do with it. Example:


<?php

// Define header
header('Content-type: text/css');

// Generate CSS
$css = 'body {' . "\n";
$css .= ' margin: 0px;' . "\n";
$css .= ' padding: 0px;' . "\n";
$css .= ' width: 100%;' . "\n";
$css .= ' min-width: 800px;' . "\n";
$css .= ' min-height: 600px;' . "\n";
$css .= '}';

// Output CSS
echo $css;
?>

This code would be viewed as a stylesheet due to the header up top. All you'd have to do is upload the file and define it as your stylesheet. You can get even more pretty if you want and name it styles.css and use a .htaccess file entry to make it parse as PHP. Just generate it from the db or whatever you need.

jjshell
01-04-2006, 08:39 PM
Thank you very much for this clear, concise and complete explanation :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum