简体   繁体   English

如何根据其中一个单元格的值更改动态填充的表格行的背景颜色?

[英]how can I change background color of dynamically filled table row based on the value of one of the cells?

I have a table being filled dynamically from mysql database. 我有一个表是从mysql数据库动态填充的。 One of the columns in the table is a date. 表中的一列是日期。 I want to change the background color of the row if the date is more than 3 days old. 如果日期已超过3天,我想更改行的背景颜色。 Trying to figure this out, I have figured out the formula and on a test.php page I can make it work with a textbox. 为了弄清楚这一点,我已经弄清楚了公式,并在test.php页面上可以使它与文本框一起使用。 I need to know how to apply it to a table row. 我需要知道如何将其应用于表格行。

function call_log_date($enter_date)
{
    $today      = strtotime(date("m/d/Y"));
    $other_date = strtotime($enter_date) . "<br>";
    // returns number of days between dates(because of abs, will always
    // return positive number)
    $diff = ceil(abs($today - $other_date) / (60 * 60 * 24));
    if ($diff > 3) {
        $bg_color   = "#FFFF00"; //yellow
        $font_color = "#FF0000"; //red
    } else if ($diff > 1 && $diff <= 3) {
        $bg_color   = "#FFFFFF"; // white
        $font_color = "#FF0000"; //red
    } else {
        $bg_color   = "#FFFFFF"; // white
        $font_color = "#000000"; //black
    }

    return $bg_color . $font_color;
}

Above is the function that I am using that works for a textbox 上面是我正在使用的功能,适用于文本框

Try it like this (untested/undebugged): 像这样尝试(未使用/未使用):

PHP: PHP:

$tdclass = call_log_date($enter_date);
echo '<td class="' .$tdclass. '">' .$enter_date. '</td>';


function call_log_date($enter_date) {
    $today = strtotime(date("m/d/Y"));
    $other_date = strtotime($enter_date)."<br>";
    $diff =  ceil(abs($today - $other_date) / (60*60*24)); //returns number of days between dates(because of abs, will always return positive number)
    if($diff > 3) {
        return 'colorA';
    }else if($diff > 1 && $diff <= 3) {
        return 'colorB';
    }else {
        return 'colorC';
    }
}

CSS: CSS:

.colorA{background-color:#FFFF00;color:#FF0000;}
.colorB{background-color:#FFFFFF;color:#FF0000;}
.colorC{background-color:#FFFFFF;color:#000000;}

This is an example how to put a color in a table row (CSS inline style): 这是一个如何在表格行中放置颜色的示例(CSS内联样式):

<table>
    <tr style="background-color: #cc6600">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
</table>

If you are creating a table dynamically, just change that color with your function. 如果要动态创建表,只需使用函数更改该颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM