0

I am new to MySQL an PHP and not sure how to do this.

I am displaying:

<?php echo "$cash"; ?>

on my page. Which works just fine.

What I would like to do is, if it is a negative number I would like the number to be red if it is a positive number I would like the number to be green the font color that is.

Any help would be appreciated, thank you.

<?php $class = ($cash < 0) ? 'red' : 'green' ?>

<p class="<?php echo $class; ?>"> <?php echo $cash; ?> </p>

<style>
.red { color: red }
.green { color: green }
</style>

this works, but i would like the '$' to be included. Example:

-$100
$100

1
  • 5
    So what attempts have you made? Taking PHP/MySQL out of the equation do you know how to do this? If so, then you just need to use PHP to dynamically modify the HTML. Commented Aug 13, 2014 at 21:55

3 Answers 3

2

In a cleaner way :

<?php $class = ($cash < 0) ? 'red' : 'green' ?>

<p class="<?php echo $class; ?>"> <?php echo $cash; ?> </p>

<style>
.red { color: red }
.green { color: green }
</style>

$cash must be an integer for the condition to work properly. So in your MySQL you must only store in the cash column the actual value. A common practice is to store on another column the currency of the ammount you're storing. Or just hardcode it like :

<p class="<?php echo $class; ?>"> $<?php echo $cash; ?> </p>

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

1 Comment

This works great. Like I said prior I am new to this. Is there a way to display -$100 in red and $100 in green. Including the '$'. It seems that if i keep the '$' in my database, it shows green. :(
1

Try this

<style>
    .red { color: red }
    .green { color: green }
</style>

<?php if ($cash < 0): ?>   
    <p class="red">- &#36; <?php echo ($cash * -1); ?></p>
<?php else: ?>    
    <p class="green">&#36; <?php echo $cash; ?></p>
<?php endif; ?>

If you have a UTF-8 document, you can write $ instead of &#36;

Comments

0

First of all, you can just do an

echo $cash

You don't need to put it in quotes.

For your question, you can do this without mySQL.

<?php
    if($cash < 0) {
        echo '<font color="red">' . $cash . '</font>';
    } else {
        echo '<font color="green">' . $cash . '</font>';
    }
?>

One other way to do this without redundant font color tags is the following

<?php
    if($cash < 0) {
        $fontColor = 'red';
    } else {
        $fontColor = 'green';
    }

    echo '<font color="' . $fontColor . '">' . $cash . '</font>';
?>

If you want to be more up to date and avoid using the old school font tags, you can use css styles.

<?php
    if($cash < 0) {
        $fontColor = 'red';
    } else {
        $fontColor = 'green';
    }

    echo '<span style="color: ' . $fontColor . ';">' . $cash . '</span>';
?>

Edit: OR, you can do what sf_tristanb suggested. As you can see there are a lot of different ways you can do this. :)

2 Comments

I had hoped that the font tag was as dead as IE4 and Netscape 4.
It's been a long time since I've done much coding in PHP, and I'm having a tough time remembering how to get this to work if you want to apply the class to an entire table instead of for one specific DB column (I've tried substituting $cash with $row, but that doesn't call the 'IF' statement). Can you do this by 'Class' instead of 'Var', and simply assign the 'Class' to the entire table and/or specific table columns?

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.