red/negative and green/positive

Asked

Viewed 172 times

-1

I have a table that, time the value is negative and, time is positive. I would like these values to be green or red. But, I couldn’t do it. Currently my select is being presented 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
        }
        }
        ?>
  • Welcome Gabriel Alves to the Stack Overflow in English. Cãso not to take a tour of the Sita in have done it would be interesting https://answall.com/tour

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
   }
   }
?>
  • Leo thank you very much, it was much simpler than I imagined and achieved exactly the goal. Last thing, on my screenRome will have many results , you find a ma practices doing it this way, (separately for each result) BUT JA VLW THANK YOU VERY MUCH.

  • @Gabrielalves, I do not think a bad practice, there is no way not to apply separately for each result even if it is with css class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.