php - 当我在表格中列出 PHP 结果时,如何在数据更改时更改边框底部颜色?

标签 php

在我的网站上,我返回了带有检查日期的房屋检查项目列表。我每天有几次检查,并按“日期时间检查”排序。当我的表格显示时,我希望在每天的最后一次检查下使用较粗的线宽或不同的线条颜色。

我非常清楚如何根据每行的数据更改属性。例如,在下面的示例代码中,我保留了使用“状态”列的数据来设置每行颜色的部分。我在这里面临的挑战是比较行之间的数据以了解在哪里更改边框底部。

<table>
    <tr style="height:30px; border: 1px solid #CCCCCC;">
        <td>Job Number</td>
        <td>Insured Name</td>
        <td>Address</td>
        <td>City</td>
        <td>County</strong></td>
        <td>Inpection Scope</strong></td>
        <td>Date of Loss</strong></td>
        <td>Inspection Date</strong></td>
    </tr>
    <? while ($row = mysqli_fetch_assoc($result1)) {
        if ($row['status'] == "Scheduled") { ?>
            <tr style="color: #16B148; ">
        <? } elseif ($row['status'] == "Closed") { ?>
            <tr style="color: #CDD4D7; text-decoration:line-through">
        <? } elseif ($row['status'] == "Inspected") { ?>
            <tr style="background-color: #92D050 ">
        <? } elseif ($row['status'] == "Delay by NI") { ?>
            <tr style="color: #9D16B1 ">
        <? } elseif ($row['status'] == "Delay by rep") { ?>
            <tr style="color: #9D16B1 ">
        <? } elseif ($row['status'] == "Tentative") { ?>
            <tr style="color: green ">
        <? } elseif ($row['status'] == "New") { ?>
            <tr style="color: red; font-weight: 250 ">
        <? } elseif ($row['status'] == "Left Voicemail") { ?>
            <tr style="color: #92D050 ">
        <? } ?>
        <td><? echo $row["claimnumber"] ?></td>
        <td><? echo $row["insuredname"] ?></td>
        <td><? echo $row["insuredaddress"] ?></td>
        <td><? echo $row["insuredcity"] ?></td>
        <td><? echo $row["insuredcounty"] ?></td>
        <td><? echo $row["inspectionscope"] ?></td>
        <td><? echo $row["dateofloss"] ?></td>
        <td><? echo $row["datetimeinspection"] ?></td>
        </tr>
    <? } ?>
</table>

最佳答案

这是示例数据的想法

<?php

//15 rows
$dates = [
'2019-08-19 00:00:00',
'2019-08-20 00:00:00',
'2019-08-21 00:00:00',
'2019-08-21 00:00:00',
'2019-08-21 00:00:00',
'2019-08-22 00:00:00',
'2019-08-22 00:00:00',
'2019-08-22 00:00:00',
'2019-08-23 00:00:00',
'2019-08-23 00:00:00',
'2019-08-23 00:00:00',
'2019-08-24 00:00:00',
'2019-08-24 00:00:00',
'2019-08-25 00:00:00',
'2019-08-25 00:00:00',
];

$lastDay = "";
$i = -1;
foreach ($dates as $date){
    $i++;
    $dateTime = new DateTime($date);
    if ($lastDay != $dateTime->format("d") && $i != 0){
        echo ($i - 1) . " is a last row in a day\n";
    }
    $lastDay = $dateTime->format("d");
}

echo $i . " is a last row in a day\n";; // last row must be a last row in a day also

此输出

0 is a last row in a day
1 is a last row in a day 
4 is a last row in a day 
7 is a last row in a day 
10 is a last row in a day 
12 is a last row in a day 
14 is a last row in a day

现场演示 ( https://3v4l.org/jc0BY )

要将其应用到您的代码示例中,您可以执行以下操作

<?php

$rows = [];
$lastDay = "";
$i = -1;
while ($row = mysqli_fetch_assoc($result1)){
    $i++;
    $dateTime = new DateTime($row['datetimeinspection']);
    $rows[] = $row;
    if ($lastDay != $dateTime->format("d") && $i != 0){
        $rows[$i - 1]['lastRowInDay'] = true;
    }
    $lastDay = $dateTime->format("d");
}
$rows[$i]['lastRowInDay'] = true;

foreach ($rows as $row){
    if (!empty ($row['lastRowInDay'])){
        //this is the last row in the day , give it the CSS that you want
         echo '<tr style="border-bottom:2px solid red">';
    } else if ($row['status'] == "Scheduled"){
        echo '<tr style="color: #16B148; ">';
    } else if ($row['status'] == "Closed"){
        echo '<tr style="color: #CDD4D7; text-decoration:line-through">';
    } else if ($row['status'] == "Inspected"){
        echo '<tr style="color: #92D050; ">';
    } else if ($row['status'] == "Delay by NI"){
        echo '<tr style="color: #9D16B1; ">';
    } else if ($row['status'] == "Tentative"){
        echo '<tr style="color: #green; ">';
    } else if ($row['status'] == "New"){
        echo '<tr style="color: red; font-weight: 250">';
    } else if ($row['status'] == "Left Voicemail"){
        echo '<tr style="color: #92D050;">';
    }
    echo '<td>' . htmlspecialchars($row["insuredname"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["insuredcity"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["insuredcounty"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["inspectionscope"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["dateofloss"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["datetimeinspection"]) . '</td>';
    echo '<td>' . htmlspecialchars($row["insuredname"]) . '</td>';
    echo '</tr>'

}

关于php - 当我在表格中列出 PHP 结果时,如何在数据更改时更改边框底部颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566587/

相关文章:

php - 处理/删除 UTF-8 从右到左覆盖字符的最佳方法是什么?

php - 存储多个复选框的最佳方法,以便我以后可以轻松查询它们

php - 如何使用 PHP 在数据库中保存正确的价格格式?

javascript - 如何将选项从 PHP 传递到 Javascript?

php - 对于存储在 DynamoDB 中的 PHP Session,我可以使用具有自动 TTL 功能的 "expires"字段吗

php - PHP我的管理员突然停止工作

php - 从一列显示多列的等效值

php - PHP中字符串的文本缩写

php - 在附加的 HTML 上使用 jQuery 效果

php - FuelPHP 的简单文件上传