Jump to content

Archived

This topic is now archived and is closed to further replies.

rgnelson

How to create .css file from form

Recommended Posts

I have many users.
Each user is logged in using session.
I would like to have form that could modify an existing css file based on logged in user.
Could someone point me in the right direction.
I have googled. I can't find a place to start  ;)

Share this post


Link to post
Share on other sites
Hi can you explain it a bit more please. Do you want to display different styles for different users?

Share this post


Link to post
Share on other sites
Hi,
Thank you,
Yes. Different styles for different users.
Each user should have their own syle sheet: {user_id}_style.css
Each user would have a form where they could modify some of the rules in the style sheet.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
That sounds like what I want to do.
So, you link to the php page?
<link href="/templates/style.php" rel="stylesheet" type="text/css">

Share this post


Link to post
Share on other sites
I'm a little lost here.
Could someone get me going in the right direction?
Any clue to get me going?
Thanks

Share this post


Link to post
Share on other sites
Use the html you posted.

In style.php
[code]<?php
file_get_contents('css/'.$_SESSION['user'].'.css');
?>[/code]

Share this post


Link to post
Share on other sites
Sounds like you will need to go through a few more PHP tutorials or do some reading before tackling such issues. As the examples provided only require the bare minimum (Basic understanding) of PHP to grasp. Good luck.

http://www.php.net

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
448191
I was wondering if you could elaborate a little more.
I would appreciate it!  :)
Thanks

Share this post


Link to post
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]

Share this post


Link to post
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]


Share this post


Link to post
Share on other sites

×

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.