0

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.

2
  • I agree. Font tags really should not be used. I put it all into a span tag in me second example. Commented Mar 20, 2013 at 14:39
  • Many thanks for all the replies - appreciate it. I have tried the solutions of Rawkode and j08691 but the font doesn't change to red. Not sure if you have any suggestions? I'll check out the other solutions. Thanks again. Commented Mar 20, 2013 at 15:54

8 Answers 8

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @j08691 - your solution worked, I must have missed something first time round. Thank you so much :-)
1

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>

Comments

0

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;
}

Comments

0

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>

Comments

0

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

Comments

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

Comments

0

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.