考虑字段值对表行进行着色

发布于 2024-12-11 11:10:14 字数 3547 浏览 1 评论 0原文

我正在开发一个公司网站,您可以在其中检索(通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2024-12-18 11:10:15

您的代码片段并没有真正解释太多,但从我看来,我会以不同的方式建议您进行切换。您的交换机正在构建 HTML 输出。

我鼓励使用 switch 来设置 $style 变量中的样式,而且...严格来说,它可以使使用 case: 首先然后使用 default 产生更好的逻辑:例如。

switch ($condition) {
  case 1 : $style = 'background:red;'; break;
  case 2 : $style = 'background:yellow;'; break;
  default : $style = 'background:white;';
}

现在构建你的 HTML 例如。

foreach ($results as $row) echo '<tr><td style="'.$style.'">Cell data</td></tr>';

对于开关上的多种情况:

switch ($condition) {
  case 0:
  case 1:
    // Do something
    break;
  case 2: /* Do something */ break;
  default: /* Do something */ break;
}

2 层开关:

switch ($condition_1) {
  case 0 :
    switch ($condition_2) {
      case 'A' :
      // do something
      break;
    }
  break;
}

不要忘记在两个级别中进行 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文