0

I'm having trouble changing the color of a value if it is a certain word, I'm using PDO to get data from a database, will be pasting the code down below.

What I want is .. if the variable " estado " is " Em analise " then the color should be red..

$stmt = $conn->prepare("SELECT * FROM orcamento");
if ($stmt->execute()) {
    echo "<div class='panel-body'>";
    echo "<div class='table-responsive'>";
    echo "<table class='table table-striped table-bordered table-hover'>";
    echo "<thead>";
    echo "<center>";
    echo "<tr>";
    echo "
    <th>#&nbsp;</th>    
    <th>Estado</th>";
    echo "</tr>";
    echo"</thead>";
    echo "<br>";
    while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
        echo "<tr>";
        echo "
        <td>$rs->CodOrc &nbsp; </td> 
        <td>$rs->estado &nbsp; </td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "Error";
}
Martijn
  • 15,791
  • 4
  • 36
  • 68

3 Answers3

0

You could add a class in your CSS:

.red { color: #f00; }

And add a class attribute of your <td> tag:

$match = $rs->estado == 'Em analise' ;
echo "<tr>";
echo "<td>$rs->CodOrc &nbsp; </td> 
      <td".($match?' class="red"':'').">$rs->estado &nbsp; </td>";
echo "</tr>";
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

As you're asking a very basic question, I'll give you the someone complex answer with some extra explanation:

echo "<td class='".($rs->estado=='Em analise'?'highlight':'')."'>".$rs->estado." &nbsp; </td>";

I did a few things here:

  • I escaped the variable. You can see in the color highlighting this reads easier. This is not really always needed, but IMO a good practice. Using the color highligher might give you a better indication where a possible bug is when you get more used to it.

  • I've added a short else if (statement ? ifTrue : ifFalse). It is a short method which allowed a simple inline check instead of a little bigger if()else{}. You can do the longer one if you prefer, but you might want to toy arround with this a bit.

  • Instead of adding a style, I've added a highlight class. Styling belongs in a stylesheet, not in code (code is for logic). You can now reuse the stlye in another table or element. Or also add pink font,some padding and/or other funky style stuff without having to change your logic.
Martijn
  • 15,791
  • 4
  • 36
  • 68
  • Thank you for the explanation! it helped me a lot! Have a great day – Pedro Miguel May 17 '18 at 18:26
  • If you like a post on StackOverflow, it recommended to upvote that post, so that others can see that it helped someone out :) That way, good answers get more visible – Martijn May 18 '18 at 08:09
0

First of all, you shouldn't echo so much HTML with PHP, simply switch between the 2 languages. I did that for you in the code below, it will allow HTML to properly highlight its own syntax so you can sniff out bugs easier. It's never a good idea to echo all of your HTML with PHP, you should only do that when necessary and if you do have to do that you should use Heredocs or similar.

Next, what you're trying to do is actually pretty easy. Simply use a ternary to check if $rs->estado is equal to the value you want, and supply a class or style to the cell.

<?
$stmt = $conn->prepare("SELECT * FROM orcamento");
if ($stmt->execute()) {
    ?>
    <div class='panel-body'>
        <div class='table-responsive'>

        <style>
            .red {
                color: red;
            }
        </style>

        <table class='table table-striped table-bordered table-hover'>
            <thead>
                <center>
                    <tr>
                        <th>#&nbsp;</th>    
                        <th>Estado</th>";
                    </tr>
            </thead>
            <tbody>
                <?php
                 while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
                    echo "<tr>";
                        echo "<td>{$rs->CodOrc} &nbsp; </td>";
                        echo "<td class='".($rs->estado == 'Em analise' ? "red" : "")."'>{$rs->estado} &nbsp; </td>";
                    echo "</tr>";
                }
                ?>
            </tbody>   
        </table>
<?php
} else {
    echo "Error";
}

The main part of this solution is the ternary, ".($rs->estado == 'Em analise' ? 'red' : '')." which basically tells PHP that if $rs->estado is equal to Em analise, it should echo the word red, and if it does not it echo's an empty string .

Note: The style tag in my answer should probably actually be added to a .css file.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71