PHP generated style sheets

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

PHP generated style sheets

Post by dhrosti »

Is it possible to generate a style sheet using a form and php? for accessibility purposes for example, i could have an "accessibility options" link to the form which will generate their personal style sheet.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Yes that's possible. However I don't think it's easy. Both for developing something like this and for the end user.

For a visitor of your site you vould provide a simple stylesheet switcher, of which there are several good ones available. Both javascript based and PHP.

Then on your webpage show a coulpel of buttons or dropdown menu to let people choose. With cookies you can make the change persistant
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

dhrosti wrote:Is it possible to generate a style sheet using a form and php? for accessibility purposes for example, i could have an "accessibility options" link to the form which will generate their personal style sheet.
Yes, it's possible.

A stylesheet is a text file that obeys some syntax rules. PHP can write text files.

Here's a very simple example of how you can create a stylesheet that allows the user to choose the background colour of the web-page:

User Form:

Code: Select all

<form id="stylesheet_creator" method="post" action="create_stylesheet.php">
 Background Colour:<input name="bg_colour" type="text" value="#FFFFFF" />
 <input type="submit" name="submit" value="Create" />
</form>
create_stylesheet.php

Code: Select all

if(isset($_POST['bg_colour'])) {
   // form is being submitted.

   // get a handle to a text file, die if we can't open it.
   if(!$handle = fopen('my_stylesheet.css', 'a')) {
      die('Unable to open file for writing!');
   }

   $bg_colour = $_POST['bg_colour'];
   // don't forget to clean any user values

   // write out the css syntax to the text file
   fwrite($handle, "body {\n");
   fwrite($handle, "\tbackground-color: ".$bg_colour.";\n");
   fwrite($handle, "}\n");
}
This script is written off the top of my head and is completely untested, so there may be a couple of errors, but the principal is there. With a little bit of tweaking (naming convention of the form fields), you could simply loop through all the entries writing the values as you go.

You would then need some way of linking the stylesheet to the user (database entry, cookie etc.), and a way of setting the stylesheet to the user's stylesheet. You can do this by using PHP to write the <link rel="stylesheet" href="$user_stylesheet" type="text/css" /> line in the HTML head.
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Thats just the sort of thing i was looking for, thanks a lot!

its not unnecessarily complex, like most posts are.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You can dynamically generate them in the same way as you dynamically generate HTML.
Filename: somecss.css.php

Code: Select all

header('Content-type: text/css');
?>
body {
   background:#faa;
}
Filename: somehtml.html

Code: Select all

<html>
  <head>
    <link rel="stylesheet" href="somecss.css.php" />
  </head>
  <body>...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

with either .htaccess or your apache config you can make php parse your CSS files. Search for "addType"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Kieran Huggins wrote:with either .htaccess or your apache config you can make php parse your CSS files. Search for "addType"
If this route is taken, remember to always include the header() content-type call. .. :)
Post Reply