Good Day,

I'm trying to changed the background color of a field based on the results of the field. My script runs fine and provides the data correctly.

I query my database and it gives me a result of $priority. $priority can be High, Medium, or Low. What I want is: If $priority is "Low", background color "Blue", $priority is "High", background color is "Red".

I'm not sure how to write the if statement in order to change a background color. Here is a snippit of my code

$query = "SELECT workorder.*, tech.techname FROM workorder RIGHT JOIN tech ON workorder.techid = tech.techid WHERE status='Open' ORDER BY date asc LIMIT $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve workorders');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	
$woid = $row['woid'];
$priority = $row['priority'];
$date = $row['date'];
$problem = $row['problem'];
$techid = $row['techid'];
$techname = $row['techname'];
$userid = $row['userid'];
$deptcode = $row['deptcode'];

	echo "<tr><td><h3><center>\n";
		echo $woid . "\n";
	echo "</h3></td><td><center>\n";
	    echo $priority . "\n";
	echo "</center></td><td>\n";
	    echo $date . "\n";
    echo "</td><td>\n";

When I "echo $priority" I want the background color based on the data $priority provides. There are 6 possible results for $priority.

Can someone point me in the right direction?

thanks.

Recommended Answers

All 6 Replies

Member Avatar for diafol

No problem - just set a class attribute to the containing tag. Suppose your values are 1,2,3,4,5,6. You could have this:

echo "...<td class=\"priority_{$priority}\">$priority</td>...";

Your css file could then have the rules:

.priority_1{
  background-color: red;
  color:white;
}
.priority_2{
  background-color: blue;
  color:white;
}
.priority_3{
  background-color: green;
  color:white;
}
.priority_4{
  background-color: orange;
}
.priority_5{
  background-color: yellow;
}
.priority_6{
  background-color: grey;
}

Obviously, change the colours to you needs - but remember that you may need to change the font colour ('color') to make the text readable on dark backgrounds.

BTW
If you want to make the whole row coloured:

echo "<tr class=\"priority_{$priority}\">....</tr>";

The css rules would then follow:

tr.priority_1 td{
  ....
}
tr.priority_2 td{
  ....
}

I put the items in my css like you said:

.priority_High{
  background-color: red;
  color:white;
}
.priority_Low{
  background-color: blue;
  color:white;
}
.priority_Medium{
  background-color: green;
  color:white;
}
.priority_On-Hold{
  background-color: orange;
}
.priority_None{
  background-color: yellow;
}
.priority_Ultra Low{
  background-color: grey;
}

and changed my coding to look like this:

$query = "SELECT workorder.*, tech.techname FROM workorder RIGHT JOIN tech ON workorder.techid = tech.techid WHERE status='Open' ORDER BY date asc LIMIT $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve workorders');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	
$woid = $row['woid'];
$priority = $row['priority'];
$date = $row['date'];
$problem = $row['problem'];
$techid = $row['techid'];
$techname = $row['techname'];
$userid = $row['userid'];
$deptcode = $row['deptcode'];

echo "<tr><td><h3><center>\n";
    echo $woid . "\n";
echo "</h3></td><td class=\"priority_{$priority}\"><center>\n";
    echo $priority . "\n";
echo "</center></td><td>\n";
    echo $date . "\n";
echo "</td><td>\n";

and nothing happened. I'm assuming I needed to change the numbers, 1,2,3,4,.... to the possible results of $priority so that is how I put it. Is this correct?

Member Avatar for diafol
.priority_Ultra Low{
  background-color: grey;
}

This won't work as it has a space in it.

Try td.priority_High etc as opposed to just .priority_High etc.
Check your html in view source to see that the html is actually displaying the attribute value (class="priority_High") as expected.

Thank you Ardav for all your help. What you gave me did work, but I had forgotten to put my css in my header, which made it not work. Its working fine now. Good fix, I really appreciate it.

Thanks again.

Yes, I caught that. I made the appropriate changes in my tables. Got to love a learning curve :) Thanks again.

Is there a way to still make this work if the value has a space in it?

My value is: UPS Ground

I checked my source to see that the html is displaying the attribute value (class="priority_UPS.Ground").

So I tried adding the period to the style

.priority_Ultra Low{
  background-color: grey;
}

But it doesn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.