1

i am trying to change the color of a td element in a table based on the statusID. status can be either zero or one. I wanna show the td as red if it its status is zero and green if status is one. my code doesn't work. I am getting the status of the device to a variable calle $statusDevice. any suggestions are welcomed.

$colors = [ '#FF0000',' #008000'];

$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db ) or die("Could not connect: " .mysqli_error($conn) );
$sql = "SELECT Name FROM parameter WHERE Device='A'";
if($result = mysqli_query($conn, $sql)){
  if(mysqli_num_rows($result) > 0){

    echo '<table>';

    while($row = mysqli_fetch_array($result)){

      $bgColor= $colors[$statusDevice];
      echo $bgColor;
      //echo $colors[0];

      echo '<tr><th>Status of Device</th>';

      echo '<td bgcolor= $bgColor); >' . $row['A']. '</td></tr>';

      echo $bgColor;

    }echo "</table>";
  }
5
  • What isn't working ? Commented Mar 26, 2019 at 14:34
  • 1
    <div style="background-color:<?=(($status%0)?'red':'yellow');?>;">blah</div> Commented Mar 26, 2019 at 14:34
  • Also do not put variables in single ticks. Check your source code. You'll figure out they are not being evaluated to their value that way. Commented Mar 26, 2019 at 14:35
  • Where's $statusDevice declared? Commented Mar 26, 2019 at 14:36
  • 1
    bgcolor is deprecated. Instead use style as @ChrisS. suggested Commented Mar 26, 2019 at 14:36

3 Answers 3

1

Use double quotes " not single ' quotes to interprete variables in strings.

change this line

  echo '<td bgcolor= $bgColor); >' . $row['A']. '</td></tr>';

to this:

  echo "<td bgcolor= $bgColor; >" . $row['A']. '</td></tr>';

And I think you have an extra parenthesis in your code (I fixed it)

Besides bgcolor is deprecated you should use style instead like So:

  echo "<td style='background-color: $bgColor;'>" . $row['A']. '</td></tr>';
Sign up to request clarification or add additional context in comments.

1 Comment

Small Note See this comment
0

You need to concatinate varibles with string:

echo '<td bgcolor= '.$bgColor.'>' . $row['A']. '</td></tr>';

Or use " instead of '

3 Comments

not sure which is more oooooldschool, using td or using the deprecated attribute bgcolor
Here is question answer, not programmin school :) I code like this at start - it normal IMHO. He will study for normal coding in process.
Not offending. Just pointing out you're using things that are likely to be removed from browsers and stop working anytime soon, if not already removed (bgcolor). Don't get me wrong there.
0

FYI, bgcolor is deprecated so just use this

echo '<td style= "background-color:'.$bgColor.';" >' . $row['A']. '</td></tr>'

instead of

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.