Page 1 of 1

PHP generated style sheets

Posted: Thu Jan 11, 2007 4:54 am
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.

Posted: Thu Jan 11, 2007 5:12 am
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

Posted: Thu Jan 11, 2007 8:45 am
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.

Posted: Thu Jan 11, 2007 8:59 am
by dhrosti
Thats just the sort of thing i was looking for, thanks a lot!

its not unnecessarily complex, like most posts are.

Posted: Thu Jan 11, 2007 10:03 am
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>...

Posted: Thu Jan 11, 2007 11:19 am
by Kieran Huggins
with either .htaccess or your apache config you can make php parse your CSS files. Search for "addType"

Posted: Thu Jan 11, 2007 11:28 am
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. .. :)