Results 1 to 5 of 5

Thread: how can we include php file within css file

  1. #1
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy how can we include php file within css file

    Hi everyone,

    I am working a color varying site now. I want to change the color for every page using CSS.

    Since, I hav a colors on my database. I jus want to call the color in the css file.

    I havn't idea that how to declare a php variable(e.g.., $color) in the style.css file.

    Pls, suggest me

    Thanks,
    Manisha

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can't.
    ...kinda.

    The solution is to output a css stylesheet with php.
    Basically...
    css.php:
    <?php
    echo "a {color:blue;}";
    ?>

    I don't think that css needs any particular format notes, like a header, but php can also output those if needed.


    Additionally, you could add .css as a php-interpreted extension... how to do so is around here somewhere... then any <?php ... ?> tags would be interpreted in .css files just like .php.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That needs to be a little trickier- since Moz/FF is picky about proper MIME type- and PHP will by default return text/html. Therefore you'll need to send
    Code:
    header('Content-type: text/css')
    to make it work right.

    After that: you'll need to assign a handler or type to css files themselves
    like in apache
    Code:
    AddType application/x-httpd-php .css

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    After that: you'll need to assign a handler or type to css files themselves
    This is inefficient, since all CSS will be parsed as PHP. Better to simply give it a .php extension:
    Code:
    <link rel="stylesheet" type="text/css" href="style.css.php">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Nov 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi
    You can iclude a php file into <head> section of your pages like that;
    <style>
    <?php include ("style.php"); ?>
    </style>

    style.php behaves like a css file now.

    Define variable $color;
    and you can use it in the style.php like a property of your css-classes like;
    body {
    color: <?echo $color;?>
    }


    In the code below, $color is changed according to page name, via variable $page. But this solution isn't useful, you must define every pages in if-else statement at the style.php.

    Content of style.php

    <?
    $page=$_SERVER["SCRIPT_NAME"];
    $color;

    if ((strpos($page, "default.php")>0)){
    $color="#000000";
    }
    else if ((strpos($page, "a.php")>0)){
    $color="#E2E2E2";
    }
    else if ((strpos($page, "b.php")>0)){
    $color="red";
    }
    ?>

    body {
    font-family:arial;
    color:<?echo $color;?>
    }

Bookmarks

Posting Permissions

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