-1

I am new to PHP trying to get data from database and check if this data equals to a value. If this is the case i wanna change the text color to red blue or green i tried the following code but if i run it my page crashes if i remove it i get all the data i want. Can someone help me out i am really new to PHP.

this is my array

$arrayValues[] = array($Names, $Title, $Danger);

this is my code

<?php
if($Danger == "High")
{
   <font color="red">$Danger</font>
}

if($Danger == "Medium")
{
   <font color="Green">$Danger</font>
}   
?>
The Codesee
  • 3,714
  • 5
  • 38
  • 78

3 Answers3

0

You need to use echo to display HTML inside PHP tags.

The font tag is not supported in HTML5. You're better off using p and changing its colour using CSS.

From your question, it seems like you're trying to loop through an array. It can be done like this:

<?php
foreach($arrayValues as $arrayValue) {
   $Danger = $arrayValue[2];
   if($Danger == "High") {
      echo'<p style="color: red;">'.$Danger.'</p>';
   }else if($Danger == "Medium") {
      echo'<p style="color: green;">'.$Danger.'</p>';
   }
}
?>
The Codesee
  • 3,714
  • 5
  • 38
  • 78
0

You can use ternary operator like:

// this equal to: if () { true } else { if () { true } else { false } }
//                         ^                     ^              ^
//                        red                   green         black (default color) 

$color = $Danger == "High" ? 'red' : ($Danger == "Medium" ? 'green' : 'black');

echo "<font color='$color'>$Danger</font>";
Aksen P
  • 4,564
  • 3
  • 14
  • 27
-1

You have to output html as a string using echo command like as:

<?php
if($Danger == "High")
{
echo "<font style=\"color:red\">" +  $Danger + "</font>";
}

... ?>