I’m having trouble getting some PHP code to work.
Here is the function:
function getCellColor($dow) {
if (isset($_POST[$dow.alternative])) {
return "style=\"background: yellow; color:#fff;\"";
}
/*elseif (isset($_POST[$dow.shifthours]) && ($_POST[$dow.shifthours] == "OFF")) {
return "style=\"background: red; color:#fff;\"";
}*/
if ($_POST[$dow.shifthours] == "OFF") {
return "style=\"background: red; color:#fff;\"";
}
else {
return "style=\"background: green; color:#fff;\"";
}
}
Here is the section that outputs to the browser:
if (isset($_POST['submit'])) {
echo preTableFrmt();
foreach($engineer as $a => $b) {
echo "| [[$engineer[$a]]] || ".getCellColor('mon')." | $monday[$a] || ".getCellColor('tues')." | $tuesday[$a] || ".getCellColor('wed')." | $wednesday[$a] || ".getCellColor('thur')." | $thursday[$a] || ".getCellColor('fri')." | $friday[$a] || ".getCellColor('sat')." | $saturday[$a] || ".getCellColor('sun')." | $sunday[$a] <br />";
}
echo postTableFrmt();
}
else { echo "Waiting for data..."; }
The “Alternative” part is working. If it is checked, it overrides everything and does the “yellow” background.
The part that doesn’t is when I leave the “Alternative” unchecked (default) and the drop down then is set to OFF (a form option from the drop down), it returns the “green” background. If I set it to another option (not OFF), it still goes to “green” background. The background needs to be red if set to OFF and anything else it should be “green”.
Form Example HTML:
<select name="tuesshifthours[]" id="tuesshifthours">
<optgroup label="Select Shift Time">
<option value="OFF">OFF</option>
<option value="8am5pm">8AM-5PM</option>
<option value="7am7pm">7AM-7PM</option>
<option value="7pm7am">7PM-7AM</option>
</optgroup>
</select>
<label for="tuesalternative">A?</label>
<input type="checkbox" name="tuesalternative[]" id="tuesalternative" value="on" />
Additionally, here is my $_POST data (returns green background):
["monshifthours"]=>
array(1) {
[0]=>
string(3) "OFF"
}
$_POST data (also returns green background):
["monshifthours"]=>
array(1) {
[0]=>
string(3) "7am7pm"
}
The problem is that your post variables are arrays and not strings. Try this: