Web Analytics Made Easy -
StatCounter PHP in CSS functions not working - CodingForum

Announcement

Collapse
No announcement yet.

PHP in CSS functions not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP in CSS functions not working

    I have php enabled in CSS and external functions do not work,

    This works:

    background-color:#<? if ('a' == 'a') { echo 'ff0000'; }?>;

    but this does not work:

    background-color:#<? if(isset($check) && ($check== 'No')) { echo 'ff0000'; }?>;

    The second one is using a function which is included from a separate file. What can I try? Thanks

  • #2
    I have php enabled in CSS and external functions do not work,
    Provide some more information, so that we can assure that your setup is OK.

    <? if ('a' == 'a') { echo 'ff0000'; }?>
    If short open tag is disabled, the above will not work.
    What's the purpose of that if statement?
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment


    • #3
      have you tried echoing $check on its own?
      You can not say you know how to do something, until you can teach it to someone else.

      Comment


      • #4
        Isn't isset() redundant there in any case? Yeah, echoing $check on its own should give you an idea as to what's wrong.
        Blog | Twitter
        Useful links: W3C HTML Validator | W3C CSS Validator | HTML 5 Guide
        CF: HTML & CSS Resources/Tutorials Thread | HTML & CSS Posting Rules and Guidelines
        Remember: no link, no code, no help!

        Comment


        • #5
          I added css handler in htaccess in the root, and added <?php header("Content-type: text/css"); ?> to the top of the .css file.

          The a=a script was for testing purposes. It works fine because it does not require any external functions.

          If I set a variable for example, $color = 'ff0000'; at the top of the css file, and then echo $color, it works as well. However my problem is when I declare the variable outside the css file, for example if $color is declared in index.php, but index.php includes the .css.

          Comment


          • #6
            If I set a variable for example, $color = 'ff0000'; at the top of the css file, and then echo $color, it works as well. However my problem is when I declare the variable outside the css file, for example if $color is declared in index.php, but index.php includes the .css.
            The following should work.
            In your php file, add
            PHP Code:
            <?php
            session_start
            ();
            $_SESSION['color']='lightblue';
            ?>
            at the top.
            Then link this file to your CSS file like
            Code:
            <link href="[ICODE]style.css.php[/ICODE]" rel="stylesheet" type="text/css" />
            In your style.css.php file add
            PHP Code:
            <?php 
            session_start
            ();
            header("Content-type: text/css");
            if(isset(
            $_SESSION['color'])) 
                
            $color=$_SESSION['color'];
            ?>
            body{
            background:<?php echo $_SESSION['color']; ?>
            }
            Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

            Comment


            • #7
              Um, try passing the variables into the link URI

              index.php
              PHP Code:
              <?php
                header
              ("Content-Type: text/html; charset=utf-8");
                
              $color "333";
                
              $background "999";
              ?>
              <!doctype html>
              <html lang="en">
                <head>
                  <link rel="stylesheet" type="text/css" href="styles.php?<?php
                
              if(isset($color)) {
                  echo 
              "&amp;color=$color";
                }
                if(isset(
              $background)) {
                  echo 
              "&amp;background=$background";
                }
              ?>" media="screen"/>
              ...
              styles.php
              PHP Code:
              <?php
              header
              ("Content-Type: text/css");
              if(isset(
              $_REQUEST['color'])) {
                
              $color $_REQUEST['color'];
              } else {
                
              $color "fff" // default color if it's not requested by index.php
              }
              if(isset(
              $_REQUEST['background'])) {
                
              $background $_REQUEST['background'];
              } else {
                
              $color "333" // default value
              }
              ?>
              html {
                color: <?php echo $color?>;
                background: <?php echo $background?>
              }
              Edit:
              Abdura got there first with sessions, which is another way to do it. You can modify his to get a "default value" type setting too.
              Last edited by Apostropartheid; Apr 3, 2009, 11:57 AM.
              Blog | Twitter
              Useful links: W3C HTML Validator | W3C CSS Validator | HTML 5 Guide
              CF: HTML & CSS Resources/Tutorials Thread | HTML & CSS Posting Rules and Guidelines
              Remember: no link, no code, no help!

              Comment

              Working...
              X