Highlighting cells on specific words

Hi all, a little stuck here and think i might have part but not all the answer.
I have cell which are populated with a department name, IE Maths / English / History blah…
I would like to change the background colour of that cell depending on the department. Here is what i have so far.

if($dept == "") {$deptcolour = "White";}
if($dept == "No") {$deptcolour = "Red";}
if($dept == "SEN") {$deptcolour = "Yellow";}
if($dept == "Admin") {$deptcolour = "Orange";}
if($dept == "Science") {$deptcolour = "Green";}
if($dept == "Maths") {$deptcolour = "blue";}

It seem to be partly working but then does highlight other cell that are not specified. Top one left blank for unselected department.

Instead of a plethora of if-statements you should use a switch statement, see the link below for a reference.
http://php.net/manual/en/control-structures.switch.php

In your case you will want to change your code to something like this (to begin with):

[php]switch($dept)
{
case “No”:
$deptcolour = “Yellow”;
break;
case “SEN”:
$deptcolour = “Red”;
break;
case “Admin”:
$deptcolour = “Orange”;
break;
case “Science”:
$deptcolour = “Green”;
break;
case “Maths”:
$deptcolour = “blue”;
break;
default:
$deptcolour = “White”;
break;
}
[/php]

It’s easy to understand and very readable/scalable. Have a look at that.

Hi Palindrome, thought there would have been an easier way to what i was doing. I seem to be learning as i go along although the next problem is always a little harder than the last :slight_smile:

thank you for you advice !

Paul

Sponsor our Newsletter | Privacy Policy | Terms of Service