简体   繁体   English

如何在PHP中的if else语句中添加CSS

[英]How to add CSS to an if else statement in PHP

I would like to change the colour of my status text based on if it is accepted (green), or rejected (red). 我想根据状态文本的接受(绿色)或拒绝(红色)来更改其颜色。 Currently, they give values of "1" being accepted or "2" being rejected. 当前,它们给出的值是“ 1”被接受或“ 2”被拒绝。

How can I change the colour of this based on the outcome. 如何根据结果更改颜色。

Here is my PHP: 这是我的PHP:

$query = "SELECT * FROM `REQUESTS` ORDER BY $selectOption"; 
echo $query;
$result = $db->query($query);
if ($result == FALSE){ 
    die ("could not execute statement $query<br />");
} else {
    echo "<form action='' method='post'>";
    echo "<table>";                         
    while($row=$result->fetchRow()){                        
        echo "<td>" . $row['status'] . "</td>";
    }
    echo "</tr>";
    echo "</table>"; 
}

Change this : 改变这个:

echo "<td>" . $row['status'] . "</td>";

By : 由:

echo "<td style='color: ".(($row['status'] == 2) ? 'red' : 'green')."'>".$row['status'].'</td>';

Create two divs with the settings. 使用设置创建两个div。

Set the reject div around reject message and accept div around the accept div. 在拒绝消息周围设置拒绝div,并在接受div周围接受div。

Not sure I understand the problem here. 不确定我在这里了解问题。

EDIT: 编辑:

Sorry, I understand now that Status is the reject/accepted. 抱歉,我现在知道状态是拒绝/接受。 I thought it was $Result 我以为是$ Result

You can add a style attribute to the <td> like this, 您可以像这样向<td>添加style属性,

$query = "SELECT * FROM `REQUESTS` ORDER BY $selectOption"; 
echo $query;
$result = $db->query($query);
if ($result == FALSE){ 
    die ("could not execute statement $query<br />");
} else {
    echo "<form action='' method='post'>";
    echo "<table>";                         
    while($row=$result->fetchRow()){

        $style = $row['status'] == 1 ? 'green' : 'red';
        echo "<td style='color:$style'>" . $row['status'] . "</td>";
    }
    echo "</tr>";
    echo "</table>"; 
}

Now is you had a CSS class's already defined called text-red and text-green you could use this 现在,您已经定义了一个名为text-redtext-green的CSS类,您可以使用它

$query = "SELECT * FROM `REQUESTS` ORDER BY $selectOption"; 
echo $query;
$result = $db->query($query);
if ($result == FALSE){ 
    die ("could not execute statement $query<br />");
} else {
    echo "<form action='' method='post'>";
    echo "<table>";                         
    while($row=$result->fetchRow()){

        $class= $row['status'] == 1 ? 'text-green' : 'text-red';
        echo "<td class='$class'>" . $row['status'] . "</td>";
    }
    echo "</tr>";
    echo "</table>"; 
}

You can do it applying different css classes depending on the state. 您可以根据状态应用不同的CSS类来完成此操作。

CSS: CSS:

td.status-accepted{color:green;}
td.status-rejected{color:red;}

HTML/PHP: HTML / PHP:

$query = "SELECT * FROM `REQUESTS` ORDER BY $selectOption"; 
echo $query;
$result = $db->query($query);
if ($result == FALSE){ 
    die ("could not execute statement $query<br />");
} else {
    echo "<form action='' method='post'>";
    echo "<table>";                         
    while($row=$result->fetchRow()){                        
    echo '<td class="'.(($row['status'] == 2) ? 'status-rejected' : 'status-accepted'). '">'.$row['status'].'</td>';
    }
    echo "</tr>";
    echo "</table>"; 
}

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

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