dounei5721 2011-10-20 06:59
浏览 16
已采纳

考虑字段值的着色表行

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).

  • 写回答

1条回答 默认 最新

  • dpzp5127 2011-10-20 07:13
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题