【问题标题】:Showing color from ternary operator显示三元运算符的颜色
【发布时间】:2020-11-25 03:27:23
【问题描述】:

如果我的三元运算符返回为真,我试图显示一种颜色,如果返回为假,则显示另一种颜色。操作员正在工作,但有一件事情发生了。双引号内的代码显示而不是实际颜色本身。有谁知道如何解决这个问题?

$msg .= ($row['opened'] == '0') ? "background-color: #DDEDFF;" : "background-color: #000000;"; 

编辑!!! - 这些是我的$msg 变量出现的唯一时间

public function getLatestMessage ($userLoggedIn, $user2) {

    $query = $this->con->prepare('SELECT body, user_to, opened, date FROM messages WHERE 
        user_to = ? AND user_from = ? OR user_to = ? AND user_from = ? ORDER BY id DESC LIMIT 1'); 
    $query->bind_param("ssss", $userLoggedIn, $user2, $user2, $userLoggedIn);
    $query->execute();
    $query_result = $query->get_result();

    $msg = ""; //to hold the message data

    if ($row = $query_result->fetch_assoc()) { //don't need a while here because we've limited the query to one record

        //Timeframe
        $date_time_now = date("Y-m-d H:i:s");
        $start_date = new DateTime($row['date']); //Time of post
        $end_date = new DateTime($date_time_now); //Current time
        $interval = $start_date->diff($end_date); //Difference between dates 
        if($interval->y >= 1) {
            if($interval->y == 1)
                $time_message = $interval->y . "yr"; //1 year ago
            else 
                $time_message = $interval->y . "yrs"; //1+ year ago
        }
        else if ($interval-> m >= 1) {
            if($interval->d == 0) {
                $days = " ago";
            }
            else if($interval->d == 1) {
                $days = $interval->d . "d";
            }
            else {
                $days = $interval->d . "d";
            }

            if($interval->m == 1) {
                $time_message = $interval->m . "month";
            }
            else {
                $time_message = $interval->m . "months";
            }

        }
        else if($interval->d >= 1) {
            if($interval->d == 1) {
                $time_message = "Yesterday";
            }
            else {
                $time_message = $interval->d . "d ";
            }
        }
        else if($interval->h >= 1) {
            if($interval->h == 1) {
                $time_message = $interval->h . "hr";
            }
            else {
                $time_message = $interval->h . "hrs";
            }
        }
        else if($interval->i >= 1) {
            if($interval->i == 1) {
                $time_message = $interval->i . "m";
            }
            else {
                $time_message = $interval->i . "m";
            }
        }
        else {
            if($interval->s < 30) {
                $time_message = "Just now";
            }
            else {
                $time_message = $interval->s . " seconds ago";
            }
        }

        $msg .= ($row['user_to'] == $userLoggedIn) ? "They said: " : "You said: ";
        $msg .= '&nbsp;&nbsp;';
        $msg .= $body = $row['body'];
        $msg .= '&nbsp;&nbsp;';
        $msg .= $time_message ;
        $msg .= '&nbsp;&nbsp;';
        $msg .= ($row['opened'] == '0') ? "background-color: #DDEDFF;" : "background-color: #000000;";
    }

    return $msg;
}

【问题讨论】:

  • 你如何使用你的msg 变量?
  • @Nicolas $msg = ""; //to hold the message data
  • Nicolas 的意思是我们看不到您在哪里使用该变量。也显示那部分代码。
  • 在我看来,您正在将文字字符串附加到您的消息中。一种选择是添加一个带有style 属性的span 标记,该属性将包含您的颜色字​​符串。
  • @Nicolas 我尝试使用&lt;p&gt; 标签,它仍然显示代码。跨度会有所不同吗?

标签: php css conditional-operator


【解决方案1】:

您的代码完全按照您所说的做:输出一个字符串,上面写着“background-color: ...”。

浏览器如何知道您想用该颜色对文本进行样式,而不是向用户呈现这些文字?

您需要为浏览器指定这是您提供的样式

代替

        $msg .= ($row['opened'] == '0') ? "background-color: #DDEDFF;" : "background-color: #000000;";

尝试类似:

 $color = $row['opened'] == '0' ? "#DDEDFF" : "#000000";
 $msg .= "<span style='background-color:$color'>THE TEXT YOU WANT TO COLORIZE</span>";

完整示例

<?php

function getLatestMessage($opened) {

    $color = $opened ? "#DDEDFF" : "#000000";
    $msg = "<span style='background-color:$color'>THE TEXT YOU WANT TO COLORIZE</span>";
    return $msg;
}

echo getLatestMessage(true);
echo getLatestMessage(false);

?>

【讨论】:

  • 刚刚也试过了$color = $row['opened'] == '0' ? "#DDEDFF" : "#000000"; $msg .= $color;
  • 我们看不到你是如何尝试实现它的,所以很难说什么是不正确的。我添加了一个完整的 sn-p,您可以尝试,它已经过测试并且可以工作。
  • 是的,确实如此。转到 phpfiddle.org 或其他沙箱,将示例粘贴进去,然后点击运行。
  • 对,因为它在一个孤立的例子中工作,这表明你有一个不同的问题。但就像我说的,我们看不到你是如何尝试实现它的。使用新代码更新您的问题。
  • 好的,它可以工作,但问题是它不是我想要着色的文本。它是一个完整的街区。我会在上面告诉你
猜你喜欢
  • 2023-03-25
  • 2015-07-31
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多