dadamssg Posted April 19, 2009 Share Posted April 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/ Share on other sites More sharing options...
JasonLewis Posted April 19, 2009 Share Posted April 19, 2009 Mmmm.. All you need to do is save it as a PHP document, obviously. But change the header to text/css like so: header("Content-type: text/css"); Then you should be able to use PHP variables in it. Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813557 Share on other sites More sharing options...
dadamssg Posted April 19, 2009 Author Share Posted April 19, 2009 save the internal sheet as an external php file? now quite sure i follow Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813564 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813567 Share on other sites More sharing options...
kenrbnsn Posted April 19, 2009 Share Posted April 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813568 Share on other sites More sharing options...
dadamssg Posted April 19, 2009 Author Share Posted April 19, 2009 sweet thanks ken...ill try that first..and then do the mime-type thing if that doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813570 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813571 Share on other sites More sharing options...
kenrbnsn Posted April 19, 2009 Share Posted April 19, 2009 Yes, I assumed that the OP's question referred to a PHP scripted page. Ken Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813572 Share on other sites More sharing options...
dadamssg Posted April 19, 2009 Author Share Posted April 19, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813573 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154717-solved-php-in-a-style-sheet/#findComment-813574 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.