deroccha
deroccha

Reputation: 1183

add a php variable to a css statement!

I would like to include the following in my css style statment. is it possible?

<option selected value="" class="" style= background: url<?php some php variable; ?></option>

Upvotes: 2

Views: 20303

Answers (3)

UltraInstinct
UltraInstinct

Reputation: 44424

Martin's answer is just fine, but I felt I should I point out that they way you are mixing CSS into the HTML isnt a good idea. Keeping the CSS part out of HTML file is always a better suggestion.

In case you would like the stylesheet to be generated for each request depending on certain values, (for eg, you need to pull values from the database), you could simply have a CSS file, and inside PHP tags you could pull the data and put it in the right place, like you would do for HTML.

For eg:

In HTML:

<link rel="stylesheet" type="text/css" href="style.php?user=abc" />

Style.php

<?php
header('Content-type: text/css');
$var = /*Get the background color setting for user abc*/;
?> 
.wrap{
    background-color:<?php echo $var; ?>;
}

Upvotes: 2

dynamic
dynamic

Reputation: 48091

That's what PHP was born for

Try echo $var;

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

If that portion of HTML code is in a .php file, yes, it is possible : PHP does just generate the output, no matter if the textual data is actually corresponding to CSS, HTML, JS, ...


Note though that your HTML code, here, is not quite OK : you are missing quotes arround the content of the style attribute, you should add parenthesis arround the url, and should use selected="selected".

Also note that you need to echo the content of the variable -- and not just use its name.


So, basically, you should have something like this :

<option selected="selected" value="" class="" 
    style="background: url(<?php echo $your_variable; ?>)">
  text of your option
</option>

Upvotes: 6

Related Questions