考虑字段值对表行进行着色
我正在开发一个公司网站,您可以在其中检索(通过 MySQL 查询)一些在 HTML 表上格式化的数据。如果特定字段值为其中一个或另一个,则尝试更改行颜色时会出现问题。
(部分)代码是:
$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>";
当尝试在 switch 语句内定义一个值,然后尝试使其成为一个条件,以便整行被着色(如果选择了情况 1 并且我们不在状态下,则为黄色)时,问题就出现了时间;如果选择了情况 2 并且我们没有准时,则为红色)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码片段并没有真正解释太多,但从我看来,我会以不同的方式建议您进行切换。您的交换机正在构建 HTML 输出。
我鼓励使用 switch 来设置 $style 变量中的样式,而且...严格来说,它可以使使用 case: 首先然后使用 default 产生更好的逻辑:例如。
现在构建你的 HTML 例如。
对于开关上的多种情况:
2 层开关:
不要忘记在两个级别中进行 BREAK
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.
now build your HTML eg.
For multiple cases on switch:
2 Tier switch:
Don't forget to BREAK in both levels