Jump to content

Recommended Posts

is it possible to use a php variable in a internal style sheet?

i want to make a background image dependent on the users gender.so i would do something like this

 

if($row['sex'] == "m")

{

$image = "malepicture.gif";

}

else

{

$image = "femalepicture.gif";

}

 

and then

 

#background

{

background-image: url(<?php echo $image; ?>);

}

 

will this work?

Link to comment
https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/
Share on other sites

in order to for the server to parse the php, it has to recognize the file type (the mime type) as a file that you want to parse php with.  If you have php installed on your server, then probably it is setup to parse php script in files of type somepage.php by default.  You can change it to recognize other mime-types in your cpanel (or other interface you may have) or on command line.  Since you are working with a template system, it would probably be easier for you to add .css to your mime-type list for parsing php, rather than trying to hack the templating system to look for your style sheet as a .php (or .html if it's as you say, internal css on some .html page or whatever).

If you have your CSS inline in the file between <style></style> tags, you can put PHP code directly in the css and it will work correcly.

 

Ken

well that would only work if the file is a .php file in the first place (or as I mentioned, another mime-type listed).  If it's on a regular .html page, he probably doesn't have the server setup to parse it on a .html file.

i have my file saved as .php i just didn't know if you could interject some php in a internal style sheet script section thats in the head.

 

like so

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>InAggieland.com | Message</title>
<link rel="stylesheet" type="text/css" href="/test/main.css" />
<style type:"text/css">
body {
margin:0;
padding: 0;
background-color: white ;
background-image: url(stripe.png) ;
background-repeat: repeat-xy;

}

#background
{
background-image: url(<?php echo $image; ?>);
}
</style>
</head>

if the page is already a php and you can write php in other places, then yes, you can do that.  your php gets parsed on the server and the results sent to the client (the browser).  As far as the browser is concerned, it's just plain text, hardcoded.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.