Martheli
Martheli

Reputation: 981

HTML change css Style Based on PHP Variable

I have a php page with html and php in it. I have the following code:

for ($x = 0; $x <= 100; $x++) {

    $result = convertTemp($x);

    $tempdesc = getTempName($x);

    $textcolor = "blue";

    echo ($x . " degrees F is equal to " . round($result, 1) . " C, which is " . '<span id="<?php $textcolor ?>" >' . $tempdesc . '</span>' ."<br/>\n");

} 

I would like to have the variable tempdesc echo in different colors based on the $textcolor variable above. For now I have it set to "blue" since I have an id set to blue in my css so they should match up and the word should be echoed in blue but the text is not changing color.

Upvotes: 1

Views: 1214

Answers (4)

Subrata
Subrata

Reputation: 57

how about using sass ? u can use variable as color or anything in it!

Upvotes: 0

Jo&#227;o Santos
Jo&#227;o Santos

Reputation: 43

Perhaps what you want is to change the inline style.

for ($x = 0; $x <= 100; $x++) {

    $result = convertTemp($x);

    $tempdesc = getTempName($x);

    $textcolor = "blue";

    echo ("{$x} degrees F is equal to {round($result, 1)} C, which is " . 
'<span style="color:{$textcolor}">' . "{$tempdesc}" . '</span>' ."<br/>\n");

} 

Make sure to use the curly braces around the variable.

Upvotes: 0

Nabeel Akbar
Nabeel Akbar

Reputation: 11

you did not have any variable ($blue)

please replace only this line

<span style="color:'.$textcolor.'" >

Upvotes: 0

user3783243
user3783243

Reputation: 5223

  1. You are already in PHP so the <?php and ?> should be removed.
  2. A variable in single quotes is the literal text, it is not the variable. In double quotes PHP expands it to the value.
  3. Your variable is $textcolor, not $blue.

Try:

echo ($x . " degrees F is equal to " . round($result, 1) . " C, which is " . '<span id="' . $textcolor . '" >' . $tempdesc . '</span>' ."<br/>\n");

Upvotes: 2

Related Questions