【问题标题】:How to implement Specific Row Coloring in PHP?如何在 PHP 中实现特定的行着色?
【发布时间】:2013-05-20 08:14:39
【问题描述】:

我正在尝试将表格列status = O 所在的所有表格行着色为黄色。而表列status = P所在的所有表行为绿色

我无法编辑当前的 IF 语句以获得我想要的输出。现在我的代码如下所示:

if ($difflogtime <= $calctimespan || ($rid == 0 && ($status == 'O' ||  $status      == 'P'))){
    echo"<tr valign='top' BGCOLOR='green'>";
    if ($editmode == 1)
        echo "<td valign='top'><Center><font face='Arial' size=2><img src='closedlock.gif' alt='Closed for editing' width='30%'></td>";
    else
        // echo "<td><center><img src='openlock.gif' alt='Open for    editing' border='0' width='30%'></td>";
        echo "<td><center><font  face='Arial' size =   2>$trimref</font></td>";
        echo "<td><Center><font  face='Arial' size =   2>".$dateshow1."</td>";
        echo "<td><Center><font face='Arial' size = 2>".$logtime."</td>";

这会将每一行显示为绿色。有人可以帮我更改此代码以获得所需的输出吗?那将不胜感激。谢谢

请注意:这是一个使用 microsoft access 数据库并运行一些简单 sql 查询的 php 文件。

【问题讨论】:

  • 我刚刚注意到您在过去几个小时内问了 3 次这个问题。您应该等待第一个问题的答案,而不是再次询问以获得更多答案。我的解决方案已经提出。如果你不能让它工作,那么你应该回到基础。

标签: php if-statement colors row


【解决方案1】:
$colors = array('O'=>'yellow',
                'P'=>'green');

if ($difflogtime <= $calctimespan || ($rid == 0 && ($status == 'O' ||  $status      == 'P'))){
  echo "<tr valign='top' BGCOLOR='{$colors[$status]}'>";
  .....

【讨论】:

    【解决方案2】:

    你可以试试这个....

    if ($difflogtime <= $calctimespan || ($rid == 0 && ($status == 'O' ||  $status == 'P'))){
        $color="";
        if ($status=="O"){$color="YELLOW";} else {$color="GREEN";}
        echo "<tr valign='top' BGCOLOR='" . $color . "'>";
    
        ....
        ....
    

    【讨论】:

      【解决方案3】:

      您的代码中没有任何东西可以检查状态然后确定颜色 我会添加这个:

      $bgColor = "yellow";
      if ($status == 'O'){
          $bgColor = "green";
      }
      

      然后使用 $bgColor 参数:

      echo '<tr valign="top" BGCOLOR="' . $bgColor . '">";
      

      无论如何,您应该尝试使用一些 CSS。您的代码风格已被弃用。 这样会好很多:

      CSS 代码:

      .yellowStyle{
          vertical-align: top;
          background-color: yellow;
       }
      
       .greenStyle{
          vertical-align: top;
          background-color: green;
        }
      

      你的 PHP/HTML:

      if ($difflogtime <= $calctimespan || ($rid == 0 && ($status == 'O' || $status == 'P')){
          $styleType = "yellowStyle";
          if ($status == 'O'){
              $styleType = "greenStyle";
          }
          echo"<tr class="' . $styleType . '">";
          ....
          ....
       }
      

      正如您所问,这是您的代码与我的解决方案相结合:

      if ($difflogtime <= $calctimespan || ($rid == 0 && ($status == 'O' || $status == 'P')){
          $bgColor = "yellow";
          if ($status == 'O'){
              $bgColor = "green";
          }
          echo '<tr valign="top" BGCOLOR="' . $bgColor . '">';
          if ($editmode == 1){
              echo "<td valign='top'><Center><font face='Arial' size=2><img src='closedlock.gif' alt='Closed for editing' width='30%'></td>";
          }
          else{
              // echo "<td><center><img src='openlock.gif' alt='Open for    editing' border='0' width='30%'></td>";
          }
          echo "<td><center><font  face='Arial' size =   2>" . $trimref . "</font></td>";
          echo "<td><Center><font  face='Arial' size =   2>" . $dateshow1 . "</td>";
          echo "<td><Center><font face='Arial' size = 2>" . $logtime . "</td>";
      }
      

      请注意,else“内容”在您的代码中并不清楚。正如你写的那样,它只会包含第一个回声,这就是我这样使用缩进的原因。

      【讨论】:

      • 我尝试将您的第一个方法输入到我的代码中,但它给了我错误。你能把你建议的代码放入我给的代码中吗?我将尝试运行它。
      • 当我运行它时它给了我这个错误:解析错误:解析错误,意外的 T_VARIABLE
      • 我只添加了颜色“if”并更改了第一行“echo”。如果您的代码在它现在应该可以工作之前工作。如果它之前不起作用,那就是另一个问题,您必须添加更多代码块。
      • 尝试查找丢失的分号或括号。您只提供了部分代码,如果我看不到其余代码,我将无能为力。
      【解决方案4】:

      试试这个

        $color = ($status=="O")? "YELLOW" : "GREEN"; 
        echo"<tr valign='top' BGCOLOR=\"$color\">";
      

      【讨论】:

      • 我应该把它放在我的代码哪里?在第一个 if 语句内还是在它之前?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2011-11-28
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多