Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP根据字符串更改表中的行颜色_Php_Html_Colors - Fatal编程技术网

PHP根据字符串更改表中的行颜色

PHP根据字符串更改表中的行颜色,php,html,colors,Php,Html,Colors,我试图让它在表中显示“添加”时变为绿色,在“删除”时变为红色,“修复”时变为蓝色。但它只是显示为默认颜色,即代码中的$color4 我的代码: <?php $db_host = 'HIDDEN'; // Server Name $db_user = 'HIDDEN'; // Username $db_pass = 'HIDDEN'; // Password $db_name = 'HIDDEN'; // Database Name $conn = mysqli_connect($db_h

我试图让它在表中显示“添加”时变为绿色,在“删除”时变为红色,“修复”时变为蓝色。但它只是显示为默认颜色,即代码中的
$color4

我的代码:

<?php
$db_host = 'HIDDEN'; // Server Name
$db_user = 'HIDDEN'; // Username
$db_pass = 'HIDDEN'; // Password
$db_name = 'HIDDEN'; // Database Name

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = 'SELECT * 
    FROM changelog ORDER BY id DESC';

$query = mysqli_query($conn, $sql);

if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<?php

function switchColor($row) { 

//Define the colors first 
$color1 = '#BAFFAE'; 
$color2 = '#AEFDFF'; 
$color3 = '#FFAEAE';
$color4 = '#DED6BA'; 

    /*Change the 'cases' to whatever you want them to be,  
    so if you want to change the color according to  
    occupation, write down the possible occupations or if  
    the color changes according to gender, name the gender 
    names that come out of the database (eg. case 'male':).*/ 

switch ($row) { 
    case 'Add': 
        echo $color1; 
        break; 
    case 'Fix': 
        echo $color2; 
        break; 
    case 'Remove': 
        echo $color3; 
        break; 
    default: 
        echo $color4; 
} 
} 

?> 





<html>
<head>
<title>Arny's Test Server | CHANGELOG |</title>
<style type="text/css">
    body {
        background-image: url(removed);
        font-size: 15px;
        color: #e1edff;
        font-family: "segoe-ui", "open-sans", tahoma, arial;
        padding: 0;
        margin: 0;
    }
    table {
        margin: auto;
        font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
        font-size: 12px;
    }

    h1 {
        margin: 25px auto 0;
        text-align: center;
        text-transform: uppercase;
        font-size: 17px;
    }

    table td {
        transition: all .5s;
    }

    /* Table */
    .data-table {
        border-collapse: collapse;
        font-size: 14px;
        min-width: 537px;
    }

    .data-table th, 
    .data-table td {
        border: 1px solid #e1edff;
        padding: 7px 17px;
    }
    .data-table caption {
        margin: 7px;
    }

    /* Table Header */
    .data-table thead th {
        background-color: #508abb;
        color: #FFFFFF;
        border-color: #6ea1cc !important;
        text-transform: uppercase;
    }

    /* Table Body */
    .data-table tbody td {
        color: #353535;
    }
    .data-table tbody td:first-child,
    .data-table tbody td:nth-child(4),
    .data-table tbody td:last-child {
        text-align: right;
    }

    .data-table tbody tr:nth-child(odd) td {
        background-color: <?php switchColor($result['type']) ?>;
    }

    .data-table tbody tr:nth-child(even) td {
        background-color: <?php switchColor($result['type']) ?>;
    }

    .data-table tbody tr:hover td {
        background-color: #ffffa2;
        border-color: #ffff0f;
    }

    /* Table Footer */
    .data-table tfoot th {
        background-color: #e5f5ff;
        text-align: right;
    }
    .data-table tfoot th:first-child {
        text-align: left;
    }
    .data-table tbody td:empty
    {
        background-color: #ffcccc;
    }
</style>
</head>
<body>
<h1>Arny's Test Server | CHANGELOG |</h1>
<table class="data-table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Type</th>
            <th>Description</th>
            <th>Platform</th>
            <th>Developer</th>
            <th>Timestamp</th>
        </tr>
    </thead>
    <tbody>
    <?php
    while ($row = mysqli_fetch_array($query))
        // Ascending Order


    {
        echo '<tr>
                <td>'.$row['id'].'</td>
                <td>'.$row['type'].'</td>
                <td>'.$row['description'].'</td>
                <td>'.$row['platform'].'</td>
                <td>'.$row['developer'].'</td>
                <td>'.$row['timestamp'].'</td>
            </tr>';
    }?>

    </tbody>

</table>
</body>
</html>

阿尼的测试服务器|变更日志|
身体{
背景图像:url(已删除);
字体大小:15px;
颜色:#e1edff;
字体系列:“segoe ui”、“开放式SAN”、tahoma、arial;
填充:0;
保证金:0;
}
桌子{
保证金:自动;
字体系列:“Lucida Sans Unicode”、“Lucida Grande”、“Segoe Ui”;
字体大小:12px;
}
h1{
保证金:25像素自动0;
文本对齐:居中;
文本转换:大写;
字号:17px;
}
表td{
过渡:全部5秒;
}
/*桌子*/
.数据表{
边界塌陷:塌陷;
字体大小:14px;
最小宽度:537px;
}
.数据表th,
.数据表td{
边框:1px实心#e1edff;
填充:7px 17px;
}
.数据表标题{
利润率:7px;
}
/*表头*/
.数据表THAD th{
背景色:#508abb;
颜色:#FFFFFF;
边框颜色:#6ea1cc!重要;
文本转换:大写;
}
/*桌体*/
.数据表tbody td{
颜色:#3535;
}
.数据表tbody td:第一个孩子,
.数据表tbody td:n子项(4),
.数据表tbody td:最后一个子项{
文本对齐:右对齐;
}
.数据表tbody tr:n子项(奇数)td{
背景色:;
}
.数据表tbody tr:n子项(偶数)td{
背景色:;
}
.数据表tbody tr:hover td{
背景色:#ffffa2;
边框颜色:#ffff0f;
}
/*表尾*/
.数据表tfoot th{
背景色:#e5f5ff;
文本对齐:右对齐;
}
.数据表tfoot th:第一个子项{
文本对齐:左对齐;
}
.数据表tbody td:空
{
背景色:#ffcccc;
}
阿尼的测试服务器|变更日志|
身份证件
类型
描述
站台
开发商
时间戳

但它不起作用。我已经尝试了很长一段时间,让这个工作,所以我将感谢您的帮助!谢谢。

您想要实现的代码不正确,即您的逻辑(代码)中存在缺陷

$array['type']
不存在,这就是为什么
switchColor
总是返回默认颜色+您以错误的方式应用了该颜色

要获得您想要的内容,您应该删除
。数据表tbody tr:nth child(奇数)td
。数据表tbody tr:nth child(偶数)td
CSS规则(它们用于不同的效果)并添加以下规则:

.data-table tbody tr td {
    background-color: #DED6BA;
}

.data-table tbody tr.Add td {
    background-color: #BAFFAE;
}

.data-table tbody tr.Fix td {
    background-color: #AEFDFF;
}

.data-table tbody tr.Remove td {
    background-color: #FFAEAE;
}
接下来,在回送
tr
的行上,应该是这样的:

echo '<tr class="' . $row['type'] . '">
echo'
此解决方案定义了几个具有不同背景样式的CSS类。我们通过在
tr
标记上设置
class
属性来选择要应用的样式。以前,您只有一种样式,并且该样式应用于表的所有行

Ah和功能
switchColor
也可以删除

更多信息:


什么是
$array
以及它是在哪里定义的?很可能这不是php的问题,而是将css正确应用于html的问题。您需要发布生成的html代码,而不是php代码。它应该是$result。很高兴它起作用了。:)但是,@Arny,请将其标记为已接受的答案,以便所有人都知道您的问题已解决。更多信息请点击此处: