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
    Apr 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 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
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

  • #4
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Isn't isset() redundant there in any case? Yeah, echoing $check on its own should give you an idea as to what's wrong.

  • #5
    New Coder
    Join Date
    Apr 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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="style.css.php" 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']; ?>
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    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; 04-03-2009 at 04:57 PM.


  •  

    Posting Permissions

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