php calculation, print result in red if neg, green if pos

Discussion in 'PHP' started by san321, Oct 26, 2007.

  1. #1
    Hi to all
    I am trying to perform a basic stock calculation.

    What I want to do is show the result in green if the amount is positive, red if the amount is negative

    <td width="53" align="center" ><font size=2><?php print ($product_info['products_sydstock'] - $product_info['products_sydcommit']); ?></font></td>
    PHP:
    If the result of the above is "-4" then I want that displayed in red(using HTML tags I presume) if the result is "99" then I want that in green.

    The idea is to make it easy for end users to see if something is in stock (green means GO, red means STOP) sort of mentality.

    Any help is much appreciated.
     
    san321, Oct 26, 2007 IP
  2. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    not sure what your variables are, but
    
    if ($variable>=0) { echo '<font color="green"> text </a>' }
    else if ($variable<0) { echo '<font color="red"> text</a> }
    
    PHP:
    or you can escape from php and just do it
     
    Lordy, Oct 26, 2007 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    
    <?php $variable = $product_info['products_sydstock'] - $product_info['products_sydcommit']; ?>
    <td width="53" align="center" ><font size="2" color="<?php echo ($variable <= 0 ? "red" : "green");?>"><?=$variable;?></font></td>
    
    PHP:
    Peace,
     
    Barti1987, Oct 28, 2007 IP