1

I have the following code in which I want to add an element to my if statement, but I am unsure how to approach this. What I am looking to do is add a background-color to the else part of it. Within this:

else {
        $status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}

So if $status is not 0, I want to set background color for the class goal-box-right.

Is there anyway I can manipulate the CSS through php like this?

foreach ($rows as $row) {
            $status = $row['status'];
            if ($status == 0) {
                $status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
            }
            else {
                $status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
            }
            $goal_date = $row['date'];
            $fixed_goal_date = fixDate($goal_date);
            $html = "";
            $html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
            $html .= '<div class="goal-box-left">'; //added
            $html .= '<div class="goal-post-status">'.$status_img. '</div>';
            $html .= '</div>'; //added
            $html .= '<div class="goal-box-right">'; //added
            $html .= '<div class="goal-post-title">'.$row['title']. '</div>';
            $html .= '<div class="goal-post-date">'.$fixed_goal_date. '</div>';
            $html .= '<div class="goal-post-description">'.$row['description']. '</div>';
            $html .= '</div>'; //added
            $html .= '</div>';
6
  • And what is the problem? Commented Nov 15, 2016 at 19:47
  • 1
    Why don't you set the background-color as an inline style? Commented Nov 15, 2016 at 19:47
  • The problem is I do not know how to set the background color to a different color through my if statement. Commented Nov 15, 2016 at 19:49
  • 1
    You use an additional css class polling a specific background color each. That way you can easily switch by php, you simply assign different classes to the element. That prevents that you have to use dynamic css which would make the css non-cachable. Commented Nov 15, 2016 at 19:51
  • @arkascha Thanks for the tip. Commented Nov 15, 2016 at 19:54

2 Answers 2

2

Separate the logic - add a CSS class based on the status then set the colours in the stylesheet.

$class = $status != 0 ? 'status-nonzero' : '';

$html .= '<div class="goal-box-right ' . $class . '">';

CSS:

.goal-box-right.status-nonzero { background-color: #foo }

Advantage is keeping the style logic in the CSS where it belongs and away from the code that's generating the HTML.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this method. I used this and it works great. Thanks again.
0

So what's the problem:

$style = '';
if ($status == 0) {
    $style = ' style="your-style:here"';
    $status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
    $status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}

// ...
$html .= '<div class="goal-box-right"' . $style . '>'; //added

1 Comment

Worked great. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.