Jump to content

How to create .css file from form


rgnelson

Recommended Posts

I have done this on my own site.

All I did was create a file style.php and used it as a style sheet on all my pages. It outputs a css file, rather than an html file. A table holds default values for all the style elements and would create a normal looking page if no overrides existed. But in querying the table it also looks for user-specific overrides as well, and used them instead wherever they existed.

So a page could offer the user the ability to enter a new value for whichever elements you opened up to them.

This is a lot easier than trying to edit style sheet files.


So I could add to my table
Link to comment
Share on other sites

Wow, that's it.  :o

I'm not asking you to write it for me.
I'm just asking for a little more detail.  ;)

I can get around in php.

I just don't see how this is the whole picture:
[code]<?php
file_get_contents('css/'.$_SESSION['user'].'.css');
?>[/code]

Just looking for a few more details.

Thanks
Link to comment
Share on other sites

I didn't actually test it (except for the regex), but here's code you could use to create user specific css files:

[code]<?php
$prop = 'color';
$style = 'green';
$element = 'body';
$file = 'css/'.$_SESSION['user'].'.css';
//Check if the user already has a preference file:
if(!file_exists($file)){
//Create a new file:
$fp = fopen($file,'w');
fwrite($fp,$element.' { '.$prop.': '.$style.'; }') or trigger_error('Couldn\'t write to file.');
}
else {
//File already exists, get contents.
$str = file_get_contents($file);
$regPattern = '/('.$element.'\s*\{[\.#@a-z:\s-;]*)('.$prop.'\s*:\s[\.#@a-z]+;)([\.#@a-z:\s-;]*\})/i';
//Check if the property is already set:
if(preg_match($regPattern)){
//If so replace the current style.
$currStyles = preg_replace($regPattern,'$1'.$prop.': '.$style.'$3',$str);
}
else {
//If not add the style.
$currStyles = preg_replace($regPattern,'$1$2'.$prop.': '.$style.'$3',$str);
}
//Get rid of old file, and write new:
unlink($file);
$fp = fopen($file,'w');
fwrite($fp,$currStyles) or trigger_error('Couldn\'t write to file.');
}
?>[/code]

I can understand if you don't immidiately grasp how it works, but I did do some heavy commenting, so you should get the basics.

$prop, $style and $element you have to assign the right values yourself in a form or where ever.

Loading css:
<link href="/css/default.css" rel="stylesheet" type="text/css">
<link href="/css/usrsheets.php" rel="stylesheet" type="text/css">

usrsheets.php:
[code]<?php
echo file_get_contents('css/'.$_SESSION['user'].'.css');
?>[/code]
Link to comment
Share on other sites

Just thought of something, if you use php to mimic a css file, you have to use the correct MIME type....

[code]<?php
header('Content-Type: text/css');
echo file_get_contents('css/'.$_SESSION['user'].'.css');
?>[/code]

Alternatively (and probably better/faster), simply assemble the HTML pointing to the right file.

[code]<?php
echo '<link href="/css/'.$_SESSION['user'].'.css" rel="stylesheet" type="text/css">';
?>[/code]

Unless ofcourse you want php to handle client/proxy caching:
[code]<?php
header('Content-Type: text/css');
header('Cache-Control: max-age=6000 must-revalidate');
echo file_get_contents('css/'.$_SESSION['user'].'.css');
?>[/code]


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.