【问题标题】:Dynamically changing row font color in table动态更改表格中的行字体颜色
【发布时间】:2014-06-26 19:44:20
【问题描述】:

我有一个名为 weather 的数据库表,其中包含以下数据:

Place       Temperature

Delhi          30
Bangalore      35
Hyderabad      45
Mumbai         41

现在我使用 php 获取这些数据并在表格中查看它们给客户端。

现在我的问题是如何根据值(即温度)动态更改行字体颜色。 我必须使用 javascript 将函数放在服务器端还是客户端。

我以这种方式应用了逻辑,但还没有到达目的地。

//here is my logic that i have put in my php file while fetching the data
if (row["Temperature"] > row["MaxTemperature"]){
    this.getStyle().color= "red"; }
    else{
    this.getStyle().color="yellow";}
   //I have set a value for MaximumTemperate 

【问题讨论】:

  • 为什么要在 PHP 中添加看似 JavaScript 的内容?

标签: php html mysql css


【解决方案1】:

你好朋友如何设置工作状态,项目名称和模块名称的颜色

$items[]=array(
             'title'=>""."Work Status:".$status."\n".'Emp Name  : '.$Emp->employee_name."[".$position."]"."\n".'Project Name : '.$project->project_name."\n".'Module Name : '.$module->module_name."",

            'start'=>$value->start_date,
            'end'=>date('Y-m-d', strtotime('+1 day', strtotime($value->end_date))),
            'color'=>'#006699',

【讨论】:

    【解决方案2】:
    <?php
    $Color = "Color:yellow;";
    if (Temp>MaxTemp)
    {
        $Color="Color:red;";
    }
    ?>
    
    <div style='<?php echo $Color ?>'>TempValue</div>
    

    【讨论】:

      【解决方案3】:

      DEMO

      common.php

      <?php
      
      $ArrayList[0] = array('place'=>'Delhi','temperature'=>30);
      $ArrayList[1] = array('place'=>'Bangalore','temperature'=>35);
      $ArrayList[2] = array('place'=>'Hyderabad','temperature'=>45);
      $ArrayList[3] = array('place'=>'Mumbai','temperature'=>41);
      
      $MaxTemperature = 40;
      
      ?>
      

      index.php

      <?php
      require_once('common.php');
      
      $Content = '
      <table border="1">
      <tr>
      <td>Place</td>`
      <td>Temperature</td>
      </tr>
      ';
      foreach ($ArrayList as $k=>$v)
      {
      $place = $v['place'];
      $temperature =  $v['temperature'];
      
      $temperature_color = ($temperature >= $MaxTemperature) ? 'color:red;' : 'color:black;';
      
      $Content .= '
      <tr>
      <td>'.$place.'</td>
      <td style="'.$temperature_color.'">'.$temperature.'</td>
      </tr>
      ';  
      }
      
      $Content .= '</table>';
      
      echo $Content;
      ?>
      

      如果您正在寻找相同的,请告诉我,我会上传文件

      【讨论】:

      • 感谢开发者对我的帮助
      【解决方案4】:

      可能是这样,如果我错了请纠正我

      if ($row['temperature'] > $row['max_temperature'])
      {
      echo "<font color='red'> red</font>";
      }
      else { echo" <font color='yellow'>yellow</font>";
      }
      

      【讨论】:

      【解决方案5】:

      我会通过创建几个 css 类来做到这一点,即:

      <style>
      .red_temperature {
      color: red;
      }
      
      .yellow_temperature {
      color:yellow;
      }
      </style>
      

      然后在您的 php 中,当您生成客户端代码时,将类添加到 div 的 class="" 中,其中包含您向客户端显示的值。

      即:

      <?php
      // set $classColor variable here based on mysql row
      ...
      
      echo "<div class='".$classColor."'>74</div>"; // where $classColor = 'yellow_temperature'
      ?>
      

      这是一个通用的起点,而不是最终的代码,只是试图用正确的思维框架为您指明正确的方向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 2017-10-30
        • 1970-01-01
        • 2011-11-20
        • 2019-09-24
        • 2017-11-02
        • 2018-04-07
        相关资源
        最近更新 更多