red / negative and green / positive

0

I have a table that, time value is negative, and time is positive. I would like these values to be green or red. But I could not do that. Currently my select is being rendered this way:

(php, myadmin, pdo)


<?php
        $sql = $pdo->prepare("SELECT sum(premio) as premio from home 
WHERE id_user = :id_user;");
        $sql->bindValue(":id_user", $id);
        $sql->execute();

        if ($sql->rowCount() > 0) {
            foreach ($sql->fetchAll() as $item) {
        ?>
     <tr>
        <th>Ganho</th>
        <td> R$ <?php echo $item['premio'];?></td>
     </tr>
     <?php
        }
        }
        ?>
    
asked by anonymous 01.01.2019 / 00:25

1 answer

4

Use a conditional to set a style to td by checking the value of $item['premio']

<?php
      $sql = $pdo->prepare("SELECT sum(premio) as premio from home WHERE id_user = :id_user;");
        $sql->bindValue(":id_user", $id);
        $sql->execute();

        if ($sql->rowCount() > 0) {
            foreach ($sql->fetchAll() as $item) {

            //valor
            $premio = $item['premio'];

                //condicional
                if ($premio<0){
                    $cor='red';
                }else{
                    $cor='green';
                }
?>
     <tr>
        <th>Ganho</th>
        <td style='color:<?php echo $cor;?>'> R$ <?php echo $premio;?></td>
     </tr>
<?php
   }
   }
?>
    
01.01.2019 / 13:48