www.webdeveloper.com
Results 1 to 13 of 13

Thread: Dynamic CSS Code

  1. #1
    Join Date
    May 2006
    Posts
    412

    Dynamic CSS Code

    Hi all i have created my own template system well i am attempting to any way i have the template information in the database in a seperate table called templates

    anyway i have this code to bring the header part of the template depending on the which template is selected in this case its the default template which is this code

    PHP Code:
    <?php 
                            
    include("dbconnect.php");
                            
    $kQuery="SELECT * FROM template Where selected='1'";
                            
    $rs=mysqli_query($con,$kQuery);
                            if(!
    $rs)
                            {
                                echo 
    "Error:".mysqli_error($con);
                            }
                            else
                            {
                                
    $count=mysqli_num_rows($rs);
                                if(
    $count>0)
                                {
                                    
    $data=mysqli_fetch_assoc($rs);
                                    
    extract($data);
                                    
    $Header=stripslashes($Header);
                                }
                                else
                                {
                                    echo 
    " Error:The Header you are Looking for cannot be found";
                                }
                            }
                    echo (
    $Error)?$Error:"";
                    echo (
    $Header)?$Header:"";    
                    
    ?>
    How can i get it to do the CSS So if i remove the CSS Style sheet from server everything will remain the same as if the style sheet is from the server in a file called Layout.CSS?

    Is this Possible maybe get some php code in the css document?

    Can this be done?

    What other Options are there for me in this case?

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Of course the stylesheet can be a php document. stylesheet.php for example or /css/index.php Don't forget to add the correct header for a stylesheet though (text/css).

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    However, browsers tend to cache stylesheets so dynamic content will tend to get lost or ignored. You'll need to provide each call to the stylesheet with a unique URL or put the styles in the HTML.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    Quote Originally Posted by Charles
    However, browsers tend to cache stylesheets so dynamic content will tend to get lost or ignored. You'll need to provide each call to the stylesheet with a unique URL or put the styles in the HTML.
    You can disable caching by sending a no-cache header and an expire header which is set in the past:
    PHP Code:
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Thur, 01 Jan 1970 00:00:00 UTC'); 

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I just tried an experiment with adding the following cache-control header to a PHP style sheet file:
    PHP Code:
    <?php
    header
    ("Content-Type: text/css");
    header("Cache-Control: no-store");
    // rest of style sheet with random color values follows....
    Seems to do the trick: http://www.charles-reace.com/test/nocache/. The stylesheet is called via a normal link tag:
    HTML Code:
    <link rel='stylesheet' href='style.php' type='text/css'>
    Tried it with both FF and IE on Win XP, and colors changed on every page load.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by pcthug
    You can disable caching by sending a no-cache header and an expire header which is set in the past.
    You can only request that the page not be cached. The granting of your request will depend upon the browser and the user settings.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by NogDog
    I just tried an experiment with adding the following cache-control header to a PHP style sheet file:
    PHP Code:
    <?php
    header
    ("Content-Type: text/css");
    header("Cache-Control: no-store");
    // rest of style sheet with random color values follows....
    Seems to do the trick: http://www.charles-reace.com/test/nocache/. The stylesheet is called via a normal link tag:
    HTML Code:
    <link rel='stylesheet' href='style.php' type='text/css'>
    Tried it with both FF and IE on Win XP, and colors changed on every page load.
    Did you first set the browsers to cache stylesheets to see if the header request overrides the user preference?
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Another way to attack the caching issue is this:
    PHP Code:
    <link rel='stylesheet' href='style.php?<?php echo time() ?>' type='text/css'>

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Quote Originally Posted by Charles
    Did you first set the browsers to cache stylesheets to see if the header request overrides the user preference?
    Presumably I'm using whatever the default settings are for that, since I don't even know where to change it for IE or FF.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    May 2006
    Posts
    245
    Hello -
    propbably I am being a bit obvious ( or just thick)
    but is the point of this so that you can write something like
    this in the style sheet :

    PHP Code:
    <?php 
    header
    ("Content-Type: text/css"); 
    header("Cache-Control: no-store"); 

    .
    bar{
      
    height$hgt;
      
    width$wdth;
      }
    ?>
    OR would it need to echo'ed ?

    PHP Code:
    <?php 
    header
    ("Content-Type: text/css"); 
    header("Cache-Control: no-store"); 

    echo 
    "
    .bar{
      height: 
    $hgt;
      width: 
    $wdth;
      }"
    :
    ?>
    Which is correct ???

    Looks pretty useful - does anyone use this ??
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  11. #11
    Join Date
    May 2006
    Posts
    245
    I looked at the example given and the style.php reads.

    body {
    margin: 0;
    padding: 1em 2em;
    font: medium arial, sans-serif;
    background-color: rgb(80%, 93%, 90%);
    color: rgb(57%, 33%, 18%);
    }
    h1 {
    background-color: rgb(84%, 87%, 84%);
    color: rgb(6%, 40%, 19%);
    }
    h2 {
    background-color: rgb(100%, 85%, 93%);
    color: rgb(27%, 16%, 10%);
    }

    This bit
    <?php
    header("Content-Type: text/css");
    header("Cache-Control: no-store");

    was not included - so now I am confused !!!

    Also where is the dynamic element ?
    I must be completely mis-understanding this
    Last edited by jeddik; 12-03-2006 at 10:18 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Here's the style.php source code in my above example:
    PHP Code:
    <?php
    header
    ("Content-Type: text/css");
    header("Cache-Control: no-store");
    ?>
    body {
       margin: 0;
       padding: 1em 2em;
       font: medium arial, sans-serif;
       <?php
       printf
    ("   background-color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(80,100), rand(80,100), rand(80,100));
       
    printf("   color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(0,60), rand(0,40), rand(0,20));
       
    ?>
    }
    h1 {
       <?php
       printf
    ("   background-color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(80,100), rand(80,100), rand(80,100));
       
    printf("   color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(0,20), rand(0,60), rand(0,40));
       
    ?>
    }
    h2 {
       <?php
       printf
    ("   background-color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(80,100), rand(80,100), rand(80,100));
       
    printf("   color: rgb(%d%%, %d%%, %d%%);\n",
              
    rand(0,40), rand(0,20), rand(0,60));
       
    ?>
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    May 2006
    Posts
    245
    Thank you
    So I wasn't barking up the wrong tree then
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles