Marcus
Marcus

Reputation: 11

Change font colour inside php echo statement depending on variable value

I am using PHP to extract data from a MySQL database into a table on a web page. This works fine but I would like to change the font of some of the output depending on the value of a variable.

The output code looks as follows:

<td><font face="Arial, Helvetica, sans-serif"><?php echo $variable; ?></font></td>

I would like to change the font to be red if the value of $variable is less than 0. Any help would be much appreciated.

Upvotes: 0

Views: 17351

Answers (8)

sinθ
sinθ

Reputation: 11493

My PHP is a bit rusty, but here is the general idea:

<td><font face="<? if ($variable == [whatever]){ ?>Arial, Helvetica, sans-serif" <? else if ($variable == [whatever]) {?> Unicorn Font <? } ?> ><?php echo $variable; ?></font></td>

So replace unicorn font with whatever font you want.

Or, as I just realized, if you want it to turn red:

<? $color = $variable < 0 : "red" ? ""> ?>
<td><font><span style="font-family: Arial, Helvetica, sans-serif; color:<? echo $color ?>"><?php echo $variable; ?></span></td>

Upvotes: 0

Adam92
Adam92

Reputation: 436

If you know all the outcomes the $variable could present, it is possible also to use a Switch statement instead of IF statements. A switch would be better to use in situations where there are a lot of predictable outcomes.

Adam92

Upvotes: 0

advermark
advermark

Reputation: 231

The FONT tag is depreciated as of HTML 4.01, you should use CSS instead.

Inline:

<td style='color:red;'><?php echo $variable; ?></td>

CSS:

.tdRed{
color:red;
}

<td class='tdRed'><?php echo $variable; ?></td>

EDIT:

To change it automatically with a certain variable, you could contain it within:

<td <?php if($variable == 'something'){ php?>class='tdRed'<?php } php?>><?php echo $variable; ?></td>

Upvotes: 1

Lowkase
Lowkase

Reputation: 5699

<td style="font face='Arial, Helvetica, sans-serif;' <?php if($variable == 0){echo "font-color:red;"} ?> ">
<?php echo $variable; } ?>
</td>

Upvotes: 0

Laurent S.
Laurent S.

Reputation: 6946

The best way to achieve this is using CSS and test value in PhP

HTML/PhP would look like :

<td<?php if($variable<0) echo 'class="subzero"'; ?>><?php echo $variable; ?></td>

and css :

td.subzero {color : #FF0000;}

Your font face should also be set in css by the way

Upvotes: 0

Rawkode
Rawkode

Reputation: 22592

Embed an if inside your font tag

<font face="Arial, Helvetica, sans-serif" <?php if ($variable < 0): ?>color="red"<?php endif; ?>><?php echo $variable; ?></font>

Upvotes: 0

j08691
j08691

Reputation: 207901

Try this:

<td><span style="font-family:Arial, Helvetica, sans-serif;<?php if($variable<0)echo "color:red"?>"><?php echo $variable; ?></span></td>

Note that the <font> tag was deprecated a long time ago so I replaced it with a <span> tag.

Upvotes: 2

jeroen
jeroen

Reputation: 91734

I would not use a font tag or inline css for that, but classes:

<td class="font_class <?php echo ($variable < 0) ? 'alert' : ''; ?>><?php echo $variable; ?></td>

and in your css:

.font_class {
  font-family: Arial, Helvetica, sans-serif;
}
.alert {
  color: red;
}

Upvotes: 0

Related Questions