0

I have a page using php/html which displays a dashboard of 8 tables built using 5 MYSQL result sets. I am wanting to change the background color of various columns based on the value contained in the columns cell. I have seen a few threads of a similar topic which have got me this far, but none of them could talk me through my specific issues, so I'm hoping someone can enlighten me. :)

Issue 1. The code below works sorta, but it is using only the first result value to define the entire columns cell color, and not individually defining each row in the column.

Issue 2. even if it was working for one result set I'm not sure how to implement it to work with all my other required result sets on the page without copying and pasting the code snippet 8 times with each different result row assigned to = $cellcolor.

In case it is relevant the rows i would like affected are...

  • $row_recordset1['check1']
  • $row_recordset1['check2']
  • $row_recordset1['check3']
  • $row_recordset2['rating']
  • $row_recordset3['check1']
  • $row_recordset3['check2']
  • $row_recordset3['check3']
  • $row_recordset4['rating']

php Snippet...

<?php
$cellcolor=$row_recordset2['rating'];
if (($cellcolor <= 100) && ($cellcolor > 85))
$color = "#C98910";
else if (($cellcolor <= 85) && ($cellcolor > 70))
$color = "#A8A8A8";
else if (($cellcolor <= 70) && ($cellcolor > 65))
$color = "#965A38";
else if ($cellcolor <= 65)
$color = "#000000";
?>

Usage snippet...

<table border="1">
<tr><td class="table_data">rating</td></tr>
<?php do { ?>
<tr class="profile" align="center">
<td class="rating" bgcolor=<?php echo $color; ?>><a href="rating.php?TargetEvent=<?php echo $row_recordset2['code1']; ?>"><?php echo $row_recordset2['rating']; ?>
</tr>
<?php } while ($row_recordset2 = mysql_fetch_assoc($recordset2)); ?>
</table>
4

2 回答 2

0

php 片段...

<?php
$cellcolor=$row_recordset2['rating'];
if (($cellcolor <= 100) && ($cellcolor > 85))
$color = "Orange";
else if (($cellcolor <= 85) && ($cellcolor > 70))
$color = "Grey";
else if (($cellcolor <= 70) && ($cellcolor > 65))
$color = "Brown";
else if ($cellcolor <= 65)
$color = "Black";
?>

使用片段...

<table border="1">
<tr><td class="table_data">rating</td></tr>
<?php do { ?>
<tr class="profile" align="center">
<td class="rating<?php echo $color; ?>" ><a href="rating.php?TargetEvent=<?php echo $row_recordset2['code1']; ?>"><?php echo $row_recordset2['rating']; ?>
</tr>
<?php } while ($row_recordset2 = mysql_fetch_assoc($recordset2)); ?>
</table>

css 文件...

.ratingOrange {
   background-color: #C98910;
}

.ratingGrey{
   background-color: #A8A8A8;
 }

.ratingBrown{
   background-color: #965A38;
}

.ratingBlack{
   background-color: #000000;
   color: #ffffff;
}
于 2013-06-04T15:48:53.417 回答
0

如果您希望每个单元格都可能具有不同的颜色,则必须在循环中包含设置颜色的逻辑。如果您有多个循环并且需要多次执行此操作,我建议将颜色逻辑放在一个函数中并从循环中调用它:

在某处定义函数:

<?php
    function get_color($cellcolor)
    {
        $color = "#ffffff";
        if (($cellcolor <= 100) && ($cellcolor > 85)) {
            $color = "#C98910";
        } else if (($cellcolor <= 85) && ($cellcolor > 70)) {
            $color = "#A8A8A8";
        } else if (($cellcolor <= 70) && ($cellcolor > 65)) {
            $color = "#965A38";
        } else if ($cellcolor <= 65) {
            $color = "#000000";
        }
        return $color
    }
?>

使用调用更新代码段以获取颜色:

<?php do { ?>
<tr class="profile" align="center">
<td class="rating" bgcolor=<?php echo get_color($row_recordset2['rating']); ?>><a href="rating.php?TargetEvent=<?php echo $row_recordset2['code1']; ?>"><?php echo $row_recordset2['rating']; ?>
</tr>
<?php } while ($row_recordset2 = mysql_fetch_assoc($recordset2)); ?>

这是一个简单的例子,说明如何在不知道确切结构/框架的情况下完成它。希望它能让你指出正确的方向......

于 2013-06-04T15:38:21.730 回答