简体   繁体   English

如果条件具有不同的颜色,我如何设置 php 样式

[英]How do I style php if conditions to have different colors

I have if conditions that display depending on the value in sql database column.我有根据 sql 数据库列中的值显示的条件。 What I am trying to achieve is to have different colors for each output.我想要实现的是每个输出都有不同的颜色。 This is my code这是我的代码

 <?php $statusshow = $row0["accStatus"]?> <p>Status:> <?php if ($statusshow == "1") {echo "Pending Confirmation!";} elseif ($statusshow == "2") {echo "Confirmed!";} elseif ($statusshow == "3") {echo "Processing..";} elseif ($statusshow == "4") {echo "Declined";} ?></p>

I would like pending confirmation to be red, confirmed to be green, processing to be yellow and declined to be red.我希望待确认为红色,确认为绿色,处理为黄色,拒绝为红色。 How can I achieve this?我怎样才能做到这一点?

To minimize codelines you can add a function which will return text message and style for provided input:要最小化代码行,您可以添加一个函数,该函数将为提供的输入返回文本消息和样式:

function getMessageAndColor($status) {
    if ($status == "1") {return ["Pending Confirmation!", "color: red;"];}
    elseif ($status == "2") {return ["Confirmed!", "color: green;"];}
    elseif ($status == "3") {return ["Processing..", "color: yellow;"];}
    elseif ($status == "4") {return ["Declined", "color: red;"];}
}

[$message, $color] = getMessageAndColor($row0["accStatus"]);?>
<p>Status:<span style="<?=$color?>"><?=$message?></p>

You can put each phrase into its own span with an inline style attribute.您可以使用内联样式属性将每个短语放入自己的跨度中。

<?php $statusshow  = $row0["accStatus"]?>
    <p>Status: <?php
     if ($statusshow == "1") {echo "<span style="color: red;">Pending Confirmation!</span>";}
     elseif ($statusshow == "2") {echo "<span style="color: green;">Confirmed!</span>";}
     elseif ($statusshow == "3") {echo "<span style="color: yellow;">Processing..</span>";}
     elseif ($statusshow == "4") {echo "<span style="color: red;">Declined</span>";}                            
     ?>

Note: The code you gave in the question started on this, giving everything red, but it failed to close the span.注意:您在问题中给出的代码开始于此,将所有内容都显示为红色,但未能关闭跨度。

Set variables in the if statements for the color and message.if语句中为颜色和消息设置变量。 Then use the variables when you echo the span.然后在回显跨度时使用变量。

$<php
$statusshow  = $row0["accStatus"];
if ($statusshow == "1") {
    $message ="Pending Confirmation!";
    $color = "red";
} elseif ($statusshow == "2") {
    $message ="Confirmed!";
    $color = "green";
} elseif ($statusshow == "3") {
    $message ="Processing..";
    $color = "yellow";
} elseif ($statusshow == "4") {
    $message ="Declined";
    $color = "red";
}
?>
<p>Status:<span style="color: <?php echo $color; ?>;"> <?php echo $message; ?></span></p>

 <?php $statusshow = $row0["accStatus"]?> <p>Status:> <?php if ($statusshow == "1") { echo "<span style='color: green'>Pending Confirmation!</span>"; } elseif ($statusshow == "2") { echo "<span style='color: green'>Confirmed!</span>"; } elseif ($statusshow == "3") { echo "<span style='color: yellow'>Processing...</span>"; } elseif ($statusshow == "4") { echo "<span style='color: red'>Declined</span>"; } ?> </p>

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

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