WordPress.org

Support

Support » How-To and Troubleshooting » Turn Negative Values in a table Red

Turn Negative Values in a table Red

  • Lucas

    @lucashjscottcom

    Here is part of a table i’m working on…..

    <tr>
    <td>$year</td>
    <td>".number_format($previous_balance)."</td>
    <td>".number_format($_POST['contribution'])."</td>

    is there some code i can add in order to make any negative values red?

    Cheers

    Lucas

Viewing 3 replies - 1 through 3 (of 3 total)
  • One way to do it is to use a function to format your numbers and wrap them with a ‘<span class=”negative”>’, then use CSS to style the negative class. Here is a sample function that could go in your functions.php or earlier in your code:

    function format_neg($val) {
       if ( intval($val) < 0 ) {
          $formatted = '<span class="negative">' . number_format($val) . '</span>';
       } else {
          $formatted = number_format($val);
       }
       return $formatted;
    }

    Then you would call that function instead of number_format().

    Lucas

    @lucashjscottcom

    Sorry for the delay, thanks for your reply vtxyzzy.
    If I add that function to my header.php file, would that mean every negative number on my site would become red?
    ps. i’m a total novice so i’m also not exactly sure how to ‘use CSS to style the negative class’ :S

    Negative numbers would be red only where you called the function, like this:

    <tr>
    <td>$year</td>
    <td>".format_neg($previous_balance)."</td>
    <td>".format_neg($_POST['contribution'])."</td>

    The function should go in your functions.php file, but be careful. If you cause an error, you cannot access your site normally, and you must use ftp to edit functions.php.

    As for the styling, add this to the end of your style.css:

    .negative { color: red; }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Turn Negative Values in a table Red’ is closed to new replies.