【问题标题】:PHP Changing cell color when pulling in mysql tablePHP在拉入mysql表时更改单元格颜色
【发布时间】:2012-07-08 00:43:01
【问题描述】:

我一直在寻找和寻找这个答案,但要么我不完全理解它(可能因为我是 php 新手),要么我无法得到正确的答案。

我有一个 php 页面,它从我的 sql 表中提取信息,该表有许多不同的列。这是代码……

$sql = "SELECT * FROM 2012_TNAfigures ORDER BY id DESC"; 
$query = mysql_query($sql) or die(mysql_error());
echo "<table class='tableDressing' id='2012table'><tbody>";
while ($row = mysql_fetch_array($query)){ 
$render2012 =  "<tr><td class='cellDressing'>".$row['weekday']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['day']."</td>
   <td class='cellDressing'>".$row['price']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td class='cellDressing'>".$row['month']."</td>
   <td style=\"color: $openColor\">".$row['open']."</td>
   <td class='cellDressing'>".$row['close']."</td>
   <td class='cellDressing'>".$row['high']."</td>
   <td class='cellDressing'>".$row['low']."</td>
   <td class='cellDressing'>".$row['changePercentage']."</td>
   <td class='cellDressing'>".$row['volume']."</td></tr>";
echo $render2012;   
}

当拉入数字时,各列将具有不同的数字,我需要根据该单元格中的值应用颜色更改。这是我为更改颜色而创建的内容......

if (($row >= 1) && ($row <= 46))
$openColor = $red;
else if (($row >= 46.01) && ($row <= 60))
$openColor = $green;

如您所见,$row 不起作用。我不知道该怎么做是拉入一列,说“打开”,在我的函数中验证该列的值,然后确定使用哪种颜色。每列也会抛出许多行,并且值会发生变化。我假设我需要将该列分配给一个变量,然后通过函数解析该变量,但我似乎无法弄清楚。

提前感谢大家。如果我应该提供更多信息,请告诉我。

【问题讨论】:

  • 您无法验证数组 $row,您必须使用其中一个值,即验证所基于的值(如 $row['open'])

标签: php mysql colors


【解决方案1】:
if (($row >= 1) && ($row <= 46))
$openColor = 'Red';
else if (($row >= 46.01) && ($row <= 60))
$openColor = 'Green';

像这样添加到您的 td 类名称

<td class="td<?php echo $openColor; ?>" >table cell data</td>

在您的 css 样式表文件中分配您想要的颜色值。我在这里使用了描述性名称,但将它们更改为您想要的十六进制颜色代码。

.tdGreen{
   background-color: green;
}

.tdRed {
   background-color: red;
}

【讨论】:

    【解决方案2】:

    $row 本身就是一个数组。不能用数字测试数组。

    if (($row['your_number'] >= 1) && ($row['your_number'] <= 46)) {
        // Do your action
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-24
      • 2019-11-16
      • 2015-03-07
      • 2020-09-25
      • 1970-01-01
      • 2011-04-12
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多