Web Analytics Made Easy -
StatCounter Change text color if number are negative - CodingForum

Announcement

Collapse
No announcement yet.

Change text color if number are negative

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Change text color if number are negative

    Hi All i`m new to php

    I want to change the text color of my row "procent" to red if i get negative numbers

    see my code below

    <!DOCTYPE html >

    <html >

    <head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">


    .TFtable{
    width:80%;
    border-collapse:collapse;
    }
    .TFtable td{
    padding:3px; border:black 1px solid;
    }
    /* provide some minimal visual accomodation for IE8 and below */
    .TFtable tr{
    background: #b8d1f3;
    }
    /* Define the background color for all the ODD background rows */
    .TFtable tr:nth-child(odd){
    background: lightgray;
    }
    /* Define the background color for all the EVEN background rows */
    .TFtable tr:nth-child(even){
    background: white;
    }
    </style>

    <table class="TFtable">

    </table>




    </style>

    </head>



    <body>


    <center>

    <?php



    $connection=mysql_connect('?','?','?') or die(mysql_error());


    mysql_select_db('?',$connection) or die(mysql_error());





    $query=mysql_query("SELECT licensnr,navn,klub,aarssnit,kampe,score,indg,(score/indg) AS snit,mp,(score/indg/aarssnit*100-100) AS procent FROM ? order by mp desc, score desc") or die(mysql_error());





    if(mysql_num_rows($query)>0):





    ?>



    <table class="TFtable" align="center">


    <tr>

    <td style="text-align:left;color:white;background-color:black;"><b>Nr.</b></td>

    <td style="text-align:left;color:white;background-color:black;"><b>Licensnr.</b></td>

    <td style="text-align:left;color:white;background-color:black;"><b>Navn</b></td>

    <td style="text-align:left;color:white;background-color:black;"><b>Klub</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>إrssnit</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Kampe</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Score</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Indg.</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Snit</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Mp</b></td>

    <td style="text-align:center;color:white;background-color:black;"><b>Diff +/-</b></td>



    </tr>



    <?php



    // looping
    $counter =1;
    while($row=mysql_fetch_object($query)):?>


    <tr>

    <td align="left"><?php echo $counter; ?></td>

    <td align="right"><?php echo $row->licensnr; ?></td>

    <td align="left"><?php echo utf8_encode($row->navn); ?></td>

    <td align="right"><?php echo $row->klub; ?></td>

    <td align="right"><?php echo $row->aarssnit; ?></td>

    <td align="right"><?php echo $row->kampe; ?></td>

    <td align="right"><?php echo $row->score; ?></td>

    <td align="right"><?php echo $row->indg; ?></td>

    <td align="right"><?php echo number_format ($row->snit,2); ?></td>

    <td align="right"><?php echo $row->mp; ?></td>

    <td align="right"><?php echo number_format ($row->procent,2); ?></td>



    </tr>

    <?php $counter++; ?>



    <?php endwhile;?>

    </table>

    </center>

    <?php

    // no result show

    else: ?>

    <h3>No Results found.</h3>

    <?php endif; ?>

    </body>

    </html>

  • #2
    Use shorthand if/else.

    Code:
    <td align="right"
    
    <?php 
    echo (number_format ($row->procent,2) < 0)? 'style="color:red" >': 'style="color:black" >';
    ?>
    
    </td>
    PS : Please, put your code into [code] tags like this.

    Comment

    Working...
    X