I'm working on a corporate website in which you retrieve (via a MySQL consult) some data that are formatted on a HTML table. The problem comes when trying to change the row color if a specific field value is one or another.
The (partial) code is:
$RowCt = 0;
while($Row = mysql_fetch_assoc($Result))
{
$timezone = new DateTimeZone( "Europe/London" );
$date = new DateTime();
$date->setTimezone( $timezone );
$ahora = $date->format( 'H:i' );
if ($var == 1) {
$rowcolor = "red";
} else if ($var == 0) {
$rowcolor = "yellow";
} else {
$rowcolor = "white";
}
//$Table.= "<tr style='background-color:#FFFFFF;'>";
foreach($Row as $field => $value)
{
switch ($field) {
default:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>";
break;
case a:
if ($now > $value) {
$var == 0;
} else {
// Do nothing
}
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>";
break;
case b:
if ($now > $value) {
$var == 1;
} else {
// Do nothing
}
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>";
break;
case c:
switch ($value) {
case 1:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
break;
case 2:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
break;
}
case d:
switch ($value) {
case 3:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
break;
case 4:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
break;
case 5:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>c</td>";
break;
case 6:
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>d</td>";
break;
}
case e:
switch ($value) {
case "E":
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
break;
case "I":
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
break;
case "F":
$Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>c</td>";
break;
}
}
}
$Table.= "</tr>";
The problem comes when trying to define a value inside of the switch statement, and then trying to make it be a condition so the entire row is colored (yellow if case 1 is selected and we aren't on time; red if case 2 is selected and we aren't on time).
Your code snippet doesnt really explain much, but from i can see, i would advise your switch differently. Your switch is building HTML output.
I would encourage using switch to rather set the style in a $style variable, also... strictly speaking it makes better logic to use case: first and then default: eg.
switch ($condition) {
case 1 : $style = 'background:red;'; break;
case 2 : $style = 'background:yellow;'; break;
default : $style = 'background:white;';
}
now build your HTML eg.
foreach ($results as $row) echo '<tr><td style="'.$style.'">Cell data</td></tr>';
For multiple cases on switch:
switch ($condition) {
case 0:
case 1:
// Do something
break;
case 2: /* Do something */ break;
default: /* Do something */ break;
}
2 Tier switch:
switch ($condition_1) {
case 0 :
switch ($condition_2) {
case 'A' :
// do something
break;
}
break;
}
Don't forget to BREAK in both levels