Change String Color upon return of Mysql

Asked

Viewed 489 times

0

I have the code below, if the score column has a field with "LOW" the color of only LOW has to be (#FFFF00) and if the field is HIGHT, only HIGHT has to have the color (#FF0000):

   <?php  
    while($row = $stm->fetch())  {
    $color = $row["score"];

   echo "<tr>"."<td><input type=checkbox name='check[]' value='[]' ></td>"."</td><td>"."<font size='1'>" .$row['quantidade']. "</td><td>". "<font size='1'>". $row['data'] . "</td><td>"."<font size='1'>" . $row['ip'] . "</td><td>"."<font size='1'>" . $row['hostname'] . "</td><td>"."<font size='1'>" .$row['sender']. "</td><td>"."<font size='1'>" .$row['subject']."</td><td>"."<font size='1'>"."<div id=add9 align=middle style='color: {$color}; text-shadow: 1px 1px 1px black, 0 0 20px blue, 0 0 1px darkblue'>" .$row['score']. " <td><a href=delete.php?id=". $row['id'] . " data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete' </a><span class='glyphicon glyphicon-trash'></span></button></p>
</tr>".'' ;
}

  switch($color){

    case 'LOW':
        $color = "#FFFF00";
        break;
    case 'HIGH':
        $color = "#FF0000";
        break;  

        return $color;
}




 ?>

What happens is that no color is changed in the field that has these cases. They could help me where I’m missing?

Grateful.

  • Do you intend to differentiate lines sequentially ? black-white-black-white-black-white, or vary depending on that field ?

  • I have a score column on my page, and wanted as each score has a color, score Higth = Red, Score Low = Yellow and Score Medium = Orange for example, only those colors have their respective color, the rest of the columns remain with the default color that is black.

1 answer

0

I was able to observe two possible problems:

1º Your Switch must be before your echo, try to create a function and call it inside the loop, below an example of how you can structure:

<?php  
function GetColor($color){
  switch($color){
    case 'LOW':
        $color = "#FFFF00";
        break;
    case 'HIGH':
        $color = "#FF0000";
        break;  
    }
    return $color;
}

while($row = $stm->fetch())  {
    $color = $row["score"];

   echo "<tr>"."<td><input type=checkbox name='check[]' value='[]' ></td>"."</td><td>"."<font size='1'>" .
   $row['quantidade']. "</td><td>". "<font size='1'>". 
   $row['data'] . "</td><td>"."<font size='1'>" . 
   $row['ip'] . "</td><td>"."<font size='1'>" . 
   $row['hostname'] . "</td><td>"."<font size='1'>" .
   $row['sender']. "</td><td>"."<font size='1'>" .
   $row['subject']."</td><td>"."<font size='1'>".
   "<div id=add9 align=middle style='color: " . GetColor($color)."; text-shadow: 1px 1px 1px black, 0 0 20px blue, 0 0 1px darkblue'>" .
   $row['score']. "</div></td><td><a href=delete.php?id=". $row['id'] . " data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete' </a><span class='glyphicon glyphicon-trash'></span></button></p></td>
</tr>".'' ;
}
?>

2º Your attribution css to color seems incorrect:

try like this

color: ".$color.";
  • I made these suggested changes @Ricardo, but it does not generate any error, but the colors do not change, continue with the default (black).

  • If you need to change the color of all line items, call the function in the TR tag as well: <tr style='color: " . Getcolor($color).";' >

  • need only the word (Hight or Medium or Low) that has the color changed, the rest remains with the standard.

  • I found another problem, change the color that is {##FFFF00} to {#FFFF00} and close the DIV that was opened in the last column

  • Hello @Ricardo, the ## was in copy&paste, in my code is correct both this and the closing of the div , ta closed here ==> darkblue'>" .

  • Try to put then the ! Mportant color: " . GetColor($color)." !important;

  • After running the page, and looking at the source code, the "Color" field is empty, see: <td><font size='1'><span style='color: '> HIGH <td>. Would you know the reason?

  • It probably did not enter any case, do the test by placing a default value switch($color){&#xA; case 'LOW':&#xA; $color = "#FFFF00";&#xA; break;&#xA; case 'HIGH':&#xA; $color = "#FF0000";&#xA; break; &#xA; default:&#xA; $color = "red"; &#xA; }&#xA;

  • If I set the default, it takes : <span style='color: red', but if I remove the default it takes the value of the color variable : <font size='1'><span style='color: HIGH'> . Very strange!

Show 4 more comments

Browser other questions tagged

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