WordPress.org

Ready to get started?Download WordPress

Forums

Turn Negative Values in a table Red (4 posts)

  1. Lucas
    Member
    Posted 11 months ago #

    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

  2. vtxyzzy
    Member
    Posted 11 months ago #

    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().

  3. Lucas
    Member
    Posted 10 months ago #

    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

  4. vtxyzzy
    Member
    Posted 10 months ago #

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

Reply

You must log in to post.

About this Topic