Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts

    how should I handle a dynamic .css file?

    Hello,

    here is the idea, I would like to have different styles available for a website. The different styles would be stored in a database. Here is the html line including the .css file:


    <link rel="stylesheet" type="text/css" href="/css/styles.css" media="screen, projection" />

    The style will be passed through the querystring (&style=). The css file should then contain php code. Problem, it can't.

    How do you handle this? What would be the best approach according to you?

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Either change the link to a.php, or config the server to treat .css as a php file (the former is 'better').
    i.e.
    <link rel="stylesheet" type="text/css" href="/css/styles.php" media="screen, projection" />
    Then something like:
    PHP Code:
    echo '#body {color:#fff;font-size:12pt;}'
    etc. (driven by your db or whatever);

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    What about MIME type? isn't the .css extension to be here for a purpose? If not, why not just name regular stylesheets with a .txt extension?


  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You can set the PHP to output whatever header you like.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    You can set the PHP to output whatever header you like.
    great post... what does that mean?

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No need to be sarcastic.

    What it means is PHP can masquerade as anything you want. CSS, images, XML, anything. A header is what tells the receiving party exactly what it is, so it knows what to do with it. Example:

    PHP Code:
    <?php

    // Define header
    header('Content-type: text/css');

    // Generate CSS
    $css 'body {' "\n";
    $css .= '    margin: 0px;' "\n";
    $css .= '    padding: 0px;' "\n";
    $css .= '    width: 100%;' "\n";
    $css .= '    min-width: 800px;' "\n";
    $css .= '    min-height: 600px;' "\n";
    $css .= '}';

    // Output CSS
    echo $css;
    ?>
    This code would be viewed as a stylesheet due to the header up top. All you'd have to do is upload the file and define it as your stylesheet. You can get even more pretty if you want and name it styles.css and use a .htaccess file entry to make it parse as PHP. Just generate it from the db or whatever you need.
    "$question = ( to() ) ? be() : ~be();"

  • #7
    New Coder
    Join Date
    Jan 2005
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you very much for this clear, concise and complete explanation


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •