Skip to content
Advertisement

Showing color from ternary operator

See hereI am trying to show a color if my ternary operator comes back as true and another one if it comes back as false. The operator is working but one thing is off. The code inside of the double quotes shows instead of the actual color itself. Anyone know how to fix this ?

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

Condition is true

Condition is false

EDIT!!! – These are the only times my $msg variable shows up

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;
}

Advertisement

Answer

Your code is doing exactly what you are telling it: outputting a string that says “background-color: … “.

How would the browser know you want to style the text with that color instead of presenting those words to the user?

You need to instead specify for the browser that this is a style you are giving it.

Instead of

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

try something like:

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

COMPLETE EXAMPLE

<?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);

?>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement