0

I'm trying to make a CSS file which alters a color based on the user that is logged in however I can't seem to run a SQL query from inside the CSS document. Doing so negates the css...

At the top of the main page I have:

<link rel="stylesheet" href="css/dynamictag.php" media="screen">

And the CSS works when the file looks like the following:

<?php
    header("Content-type: text/css; charset: UTF-8");
?>
#colortag {
    width: 30px;
    height: 50px;
    float: left;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    -webkit-box-shadow: 1px 0px 5px;
    box-shadow: 1px 0px 5px;
    margin-left: 5px;
    margin-top: -5px;
    background-color: #FFFFFF;
    margin-right: 5px;
}
#theuser {
    float: left;
    font-size: small;
}

But it doesn't apply whatsoever when I have the following:

<?php
    header("Content-type: text/css; charset: UTF-8");
?>
<?php
$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';"
        $result=mysql_query($query);
        $row=mysql_fetch_array($result);
        $color='#'.$row['Color'];
?>
#colortag {
    width: 30px;
    height: 50px;
    float: left;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    -webkit-box-shadow: 1px 0px 5px;
    box-shadow: 1px 0px 5px;
    margin-left: 5px;
    margin-top: -5px;
    background-color: <?php echo $color;?>;
    margin-right: 5px;
}
#theuser {
    float: left;
    font-size: small;
}

Is there another means of doing this? Am I doing something wrong?

Thanks

5
  • Maybe a cache issue. Try using href="css/dynamictag.php?<?php rand();?>" in your link tag. If not then check or post the css output. Commented Mar 14, 2014 at 4:12
  • No luck there unfortunately. Commented Mar 14, 2014 at 4:15
  • Check the generated css output then and see if that's as it should be. If it is then see what "background-color: <?php echo $color;?> !important;" does. Commented Mar 14, 2014 at 4:20
  • Note the missing echo in my first comment. That should have been href="css/dynamictag.php?<?php echo rand();?>" Commented Mar 14, 2014 at 4:22
  • Still no luck there. And it's not just that the color isn't being applied its that the entire style sheet is ignored. Commented Mar 14, 2014 at 4:26

1 Answer 1

1

The following line is missing a semicolon at the end (and instead has it inside the SQL statement), so the PHP is not executing correctly.

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';"

It should be:

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session'";

Also, you should be using mysqli or PDO. Using mysql_query in PHP is deprecated, as seen on the mysql_query PHP page.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, happens to the best of us :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.