Jump to content


Photo

How to create .css file from form


  • Please log in to reply
14 replies to this topic

#1 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 03:29 PM

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  ;)

#2 syed

syed
  • Members
  • PipPipPip
  • Advanced Member
  • 151 posts
  • LocationEngland

Posted 23 August 2006 - 03:34 PM

Hi can you explain it a bit more please. Do you want to display different styles for different users?

#3 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 03:47 PM

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.


#4 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 23 August 2006 - 04:22 PM

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
If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#5 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 04:27 PM

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">

#6 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 08:12 PM

I'm a little lost here.
Could someone get me going in the right direction?
Any clue to get me going?
Thanks

#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 08:19 PM

Use the html you posted.

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


#8 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 08:38 PM

Sorry,
I'm new.
There wasn't enough for me to go on.
Thanks.

#9 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 23 August 2006 - 08:50 PM

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
PHP Ninja

#10 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 08:56 PM

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:
<?php
file_get_contents('css/'.$_SESSION['user'].'.css');
?>

Just looking for a few more details.

Thanks

#11 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 09:25 PM

Is it a secret or is it too simple to exlain?

#12 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 10:28 PM

448191
I was wondering if you could elaborate a little more.
I would appreciate it!  :)
Thanks

#13 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 10:30 PM

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

<?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.');
}
?>

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:
<?php
echo file_get_contents('css/'.$_SESSION['user'].'.css');
?>


#14 rgnelson

rgnelson
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 23 August 2006 - 10:35 PM

448191
Thank you,
This gives me a start at my goal.
Thanks again!
:)




#15 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 24 August 2006 - 07:42 AM

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

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

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

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

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







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users